CentOS stram 9:pm2 の自動起動

1. 環境
・CentOS stram 9
2. pm2の自動起動
(1) pm2でそれぞれのサービスを起動させておく

# cd /opt/ProvateInvocie/166S
# pm2 index.js --name 166S
# cd /opt/ProvateInvocie/166C
# pm2 index.js --name 166C
# pm2 list
lqqqqwqqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqwqqqqqqwqqqqqqqqqqqwqqqqqqqqqqwqqqqqqqqqqk
x id x name               x mode     x ?    x status    x cpu      x memory   x
tqqqqnqqqqqqqqqqqqqqqqqqqqnqqqqqqqqqqnqqqqqqnqqqqqqqqqqqnqqqqqqqqqqnqqqqqqqqqqu
x 1  x 166C               x fork     x 0    x online    x 0%       x 63.5mb   x
x 2  x 166S               x fork     x 0    x online    x 0%       x 66.5mb   x
mqqqqvqqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqvqqqqqqvqqqqqqqqqqqvqqqqqqqqqqvqqqqqqqqqqj

(2) PM2起動スクリプト
・pm2 save
・~/.pm2/dump.pm2に作成される

# pm2 save
# ls -l ~/.pm2/dump.pm2
-rw-r--r--. 1 root root 19972 Apr 16 14:05 /root/.pm2/dump.pm2

3. pm2でサービスの作成と確認
・pm2 startup centos

# pm2 startup centos
# ls /etc/systemd/system/pm2-root.service
# cat /etc/systemd/system/pm2-root.service
[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=/root/.local/bin:/root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/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/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target
[root@localhost 166C]#

4. 注意
新しいNode.jsバージョンにアップグレードする場合は、PM2起動スクリプトを更新する必要がある。

# pm2 unstartup
# pm2 startup