[CentOS] PM2 서비스 등록

리눅스 환경에서 PM2 서비스 등록하는 방법에 대해 알아보겠습니다.

PM2에서는 시스템이 부팅되고 나서 자동으로 실행되도록 하는 서비스를 등록해 주는 startup 명령어가 있습니다.

pm2 등록

1
2
$ pm2 start [파일명] --name test
$ pm2 list

서비스 등록

pm2 startup 명령어를 사용하여 PM2 서비스를 등록합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$ pm2 startup
[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

pm2 save 명령어를 통해 재부팅 시 프로세스 목록을 고정시킵니다.

1
2
3
$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2

PM2 서비스를 시작합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
$ sudo systemctl start pm2-root.service
$ systemctl status pm2-root.service
● pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: disabled)
Active: active (running) since 금 2022-12-09 17:14:27 KST; 5min ago
Docs: https://pm2.keymetrics.io/
Process: 18456 ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
Main PID: 13515 (PM2 v5.2.0: God)
Tasks: 0
Memory: 0B
CGroup: /system.slice/pm2-root.service
‣ 13515 PM2 v5.2.0: God Daemon (/root/.pm2)
...

시스템 재부팅 후 서비스를 확인합니다.

1
$ sudo reboot
1
$ pm2 list
Share