How to Automatically Restore PM2 Apps After Server Reboot (Managed Hosting)<
How to Automatically Restore PM2 Apps After Server Reboot (Managed Hosting)
Overview
On managed servers without root access, PM2 processes do not automatically restart after a server reboot. This can cause all Node.js / Next.js applications to go offline.
To solve this, we use:
- PM2 process snapshot (
pm2 save) - Crontab @reboot to restore apps
- Optional watchdog cron to ensure uptime
Step 1: Save Current PM2 State
Run this once after all applications are running:
pm2 saveThis creates a snapshot file (~/.pm2/dump.pm2) containing all running apps.
Step 2: Find the Correct PM2 Path
Cron jobs run in a limited environment, so you must use the full path to PM2.
Run:
which pm2Example output:
/home/username/.volta/bin/pm2
/usr/bin/pm2
/home/username/.nvm/versions/node/v18/bin/pm2Copy the full path returned and use it in your cron job.
Step 3: Add Crontab Entry for Reboot
Edit your crontab:
crontab -eAdd the following line (replace /full/path/to/pm2):
@reboot /usr/bin/env bash -lc '/full/path/to/pm2 resurrect >> $HOME/pm2_reboot.log 2>&1'This ensures that all saved PM2 applications are restored automatically after a server restart.
Step 4: Add Watchdog Cron (Optional but Recommended)
This cron job ensures that if PM2 or apps stop unexpectedly, they are restored.
*/15 * * * * /usr/bin/env bash -lc '/full/path/to/pm2 resurrect >> $HOME/pm2_watchdog.log 2>&1'This runs every 15 minutes and has minimal performance impact.
Alternative: Using Dynamic Path (Optional)
If your environment supports it, you can dynamically resolve PM2:
@reboot /usr/bin/env bash -lc '$(which pm2) resurrect'Note: This may not work on all managed hosting environments due to PATH limitations. Using a fixed full path is more reliable.
How It Works
pm2 save→ stores current appspm2 resurrect→ restores saved apps@reboot→ runs on server startupcron→ ensures recovery if something fails later
Testing
Test manually without reboot:
pm2 kill
pm2 resurrectCheck logs after reboot:
cat $HOME/pm2_reboot.logVerify running apps:
pm2 listImportant Notes
- Always run
pm2 saveafter adding/removing apps - Use absolute paths in cron for reliability
- Avoid relying on
~or PATH in cron - Logs help debug issues after reboot
Summary
By combining PM2 snapshot and cron jobs, you ensure that all Node.js applications automatically recover after server restarts, even without root access.