Advertisement
Intermediate Node.js, Deployment & Process Managers (PM2)

How do you configure PM2 to automatically restart your Node.js application after a server reboot?

Short answer

PM2's 'startup' command generates and configures a system-specific init script (systemd on most modern Linux distributions) that starts PM2 itself automatically on system boot; combined with 'pm2 save' (which persists the current list of running processes to a dump file), PM2…

Save Node.js mock interview
Advertisement
AModel answer
PM2's 'startup' command generates and configures a system-specific init script (systemd on most modern Linux distributions) that starts PM2 itself automatically on system boot; combined with 'pm2 save' (which persists the current list of running processes to a dump file), PM2 will automatically restore and restart every previously running application after the server reboots, without any process being lost.
Example

pm2 startup # generates and prints a system-specific command to run # (run the printed command, e.g. with sudo, to actually register the startup script) pm2 save # saves the currently running process list # After a reboot, PM2 automatically restores everything from this saved list

Real-world example
A production server that was manually restarted for a security patch came back up with the Node.js API not running at all until an engineer noticed and manually started it again; running 'pm2 startup' and 'pm2 save' beforehand ensures the application resumes automatically on any future reboot without manual intervention.
Likely follow-ups
  • What happens if 'pm2 save' isn't run after adding a new application, and the server later reboots?
  • How does this systemd-based startup mechanism differ from relying on a Docker container's own restart policy in a containerized deployment?
Related topics