Linuxnode.js

PM2 Startup Hook

By November 11, 2018 No Comments

PM2 Runtime is a Production Process Manager for Node.js applications with a built-in Load Balancer. It allows you to keep applications alive forever, to reload them without downtime and facilitate common Devops tasks.

Last year it became our default Process Manager for Node.js applications and it’s worked perfectly.  The only time it does go down and not recover is after a server reboot.  They don’t happen often and when they do it’s normally us applying server updates.  The few times a year it’s rebooted unexpectedly the process will not come back online until we manually SSH in and run the start command:

pm2 start script.js

Again, while it didn’t occur often, it was becoming a nuisance and of course would happen at the most inconvenient times.  PM2 includes a startup hook that saves your process list and restarts them on system reboots.

Login via SSH and as root run

pm2 startup

Once that’s complete you’ll need to save off the process list you want to have brought back online after a server reboot.  So go ahead and spin up any of the processes with pm2 start and then run:

pm2 save

That’s it, reboot your server and watch the process come back online automatically.

Leave a Reply