The better way of doing it is use the pm2 startup
command
http://pm2.keymetrics.io/docs/usage/startup/
To get the automatically-configured startup script for your machine you need to type this command:
# Detect available init system, generate configuration and enable startup system
pm2 startup
You can specify the platform you use by yourself if you want to (where platform can be either one of the cited above):
pm2 startup [ubuntu | ubuntu14 | ubuntu12 | centos | centos6 | arch | oracle | amazon | macos | darwin | freebsd | systemd | systemv | upstart | launchd | rcd | openrc]
The output of this command can be a recommendation of the line to copy/paste with all environment variables and options configured for you.
Example:
[PM2] You have to run this command as root. Execute the following command:
sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v4.3/bin pm2 startup -u --hp
You simply have to copy/paste the line PM2 gives you and the startup script will be configured for your OS.
Once you run the sudo pm2 startup
. It will create the systemctl service
. You can check the status of the same using
systemctl status pm2-root
By default the service is not configure to restart automatically. You will run the below commands
sudo mkdir -p /etc/systemd/system/pm2-root.service.d
and then create a file name 10_auto_restart_pm2.conf
with below content
[Service]
Restart=always
RestartSec=3
After that execute
systemctl daemon-reload
systemctl restart pm2-service
Now let's test the auto restart part
$ 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)
Now we kill the process manually and wait for 3 seconds
$ 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)
As you can see the process/service was restarted automatically. No cron needed and it is how you should do it.