PM2 Node.js Process Manager Cheat Sheet¶
PM2 is a process manager for keeping applications running and controlling them from one CLI.
Install PM2¶
Node.js and npm must already be installed:
npm install pm2@latest -g
pm2 --version
Start an Application¶
pm2 start app.js --name my-app
PM2 can also start an ecosystem file:
pm2 start ecosystem.config.js
Manage Processes¶
pm2 list
pm2 describe my-app
pm2 restart my-app
pm2 reload my-app
pm2 stop my-app
pm2 delete my-app
Use the process name or the numeric ID shown by pm2 list.
View Logs¶
Show all live logs:
pm2 logs
Show one application's logs and include the last 200 lines:
pm2 logs my-app --lines 200
Use Ctrl+C to leave the live log view; this does not stop the application.
Restore Processes After Reboot¶
Ask PM2 to detect the init system:
pm2 startup
PM2 prints a command containing the correct user, home directory and Node.js path. Copy and run the command it prints instead of using a hard-coded command from another machine.
Start every application that should return after reboot, then save the current list:
pm2 save
Restore a saved list manually:
pm2 resurrect
Remove the startup configuration:
pm2 unstartup
Update PM2¶
npm install pm2@latest -g
pm2 update
After changing the installed Node.js version, regenerate the startup script so that it uses the new Node.js path:
pm2 unstartup
pm2 startup
Run the command printed by each operation, then save the process list again if it changed.