DevOps Discussion

PM2 — Restart Processes After System Reboot

Startup Script Generation

PM2 ships with the functionality to generate startup scripts for multiple init systems. These scripts are executed on system boot and with that spawn the PM2 process itself which is required to (re)start application servers.

You can generate a startup script for your server’s init system by either using PM2’s auto-detection feature or pass a specific init system name to the startup command. If you don’t know your init system, just run pm2 startup without arguments to let PM2 auto-detect.

pm2 startup  
# or
pm2 startup <init-system>  
# <init-system> is one of these options
# systemd|upstart|launchd|rcd|systemv

The startup command will generate another command which you need to execute as root on your system.

Since we use an Ubuntu (14.04) box to test the restart on boot feature, we precisely pass upstart as the init system’s name to make sure we don’t get caught in a nasty bug caused by not defining the init-system.

$ pm2 startup upstart
[PM2] You have to run this command as root. Execute the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup upstart -u vagrant --hp /home/vagrant

Execute the generated command to daemonize PM2 and generate the system’s init script which is executed on boot.

$ sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup upstart -u vagrant --hp /home/vagrant
Platform upstart  
Template  
#!/bin/bash
…
[PM2] Writing init configuration in /etc/init.d/pm2
[PM2] Making script booting at startup...
…
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

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

PM2 is configured to be started on boot, but we didn’t have any application to restart. Let’s start a Node.js server with PM2.

Start Node.js Server

You can use any of your personal applications or just create a new one for testing purposes.

We use the exemplary code from Node’s startpage and kick off the server with PM2.

$ pm2 start server.js
[PM2] Starting server.js in fork_mode (1 instance)
[PM2] Done.
---------------------------------------------------------------------------------------------------
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu │ memory     │ watching │
---------------------------------------------------------------------------------------------------
│ server   │ 0  │ fork │ 2065 │ online │ 0       │ 0s     │ 22% │ 6.664 MB   │ disabled │
---------------------------------------------------------------------------------------------------

Save Processes for Restart on Boot

There is one more thing to do: save the process list which should be restarted on system boot.

$ pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /home/vagrant/.pm2/dump.pm2

This dumps the process list to disk which can be restored easily. The process list gets populated into ~/.pm2/dump.pm2and is accessable for PM2 for process resurrection.

That’s all the magic. Go ahead and see if the processes restart after system reboot.

What if Processes Don’t Start on Boot?

We had issues getting PM2 itself and the managed processes to be restored on system boot and the reason was wrong permissions. PM2 itself should be started with system boot which requires root privileges to be kicked off with other system services. However, the application processes run within the scope of another system user, let’s call him nodeuser.

We solved the boot issue by passing the -u nodeuser parameter to PM2’s startup command. That concretely defined nodeuser as the user for which we want to resurrect the saved processes.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *