The best way to do this is to use the command pm2 startup
http://pm2.keymetrics.io/docs/usage/startup/
To get an automatically configured script run for your computer, you need to enter the following command:
pm2 startup
You can specify the platform that you use yourself if you want (where the platform can be either one of the above):
pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freebsd | systemd | systemv | upstart | launchd | rcd | openrc]
/ , .
:
[PM2] root. : sudo su -c "env PATH = $PATH:/home/unitech/.nvm/versions/ node/v4.3/bin pm2 startup -u -hp
/ , PM2 , script .
sudo pm2 startup. systemctl service. ,
systemctl status pm2-root
.
sudo mkdir -p /etc/systemd/system/pm2-root.service.d
10_auto_restart_pm2.conf
[Service]
Restart=always
RestartSec=3
systemctl daemon-reload
systemctl restart pm2-service
$ systemctl status pm2-root.service
ā pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/pm2-root.service.d
āā10_auto_restart_pm2.conf
Active: active (running) since Wed 2018-02-28 16:52:19 UTC; 11s ago
Docs: https://pm2.keymetrics.io/
Process: 5014 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
Process: 5022 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
Main PID: 5031 (PM2 v2.10.1: Go)
Tasks: 9
Memory: 24.3M
CPU: 460ms
CGroup: /system.slice/pm2-root.service
āā5031 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)
3
$ kill -9 5031
$ sleep 3
$ systemctl status pm2-root.service
ā pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: enabled)
Drop-In: /etc/systemd/system/pm2-root.service.d
āā10_auto_restart_pm2.conf
Active: active (running) since Wed 2018-02-28 16:52:55 UTC; 641ms ago
Docs: https://pm2.keymetrics.io/
Process: 5057 ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill (code=exited, status=0/SUCCESS)
Process: 5081 ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect (code=exited, status=0/SUCCESS)
Main PID: 5088 (PM2 v2.10.1: Go)
Tasks: 9
Memory: 24.3M
CPU: 461ms
CGroup: /system.slice/pm2-root.service
āā5088 PM2 v2.10.1: God Daemon (/home/vagrant/.pm2)
, / . cron, .