| name | pm2 |
| description | Manage Node.js applications with PM2 process manager. Use for deploying, monitoring, and auto-restarting Node apps in production. Covers starting apps, viewing logs, setting up auto-start on boot, and managing multiple processes. |
PM2 Process Manager
Production process manager for Node.js with built-in load balancer.
Install
npm install -g pm2
Quick Start
pm2 start app.js
pm2 start npm --name "my-app" -- start
pm2 start "npm run start" --name my-app
pm2 start npm --name "my-app" -- start -- --port 3000
PORT=3000 pm2 start npm --name "my-app" -- start
Common Commands
pm2 list
pm2 ls
pm2 logs
pm2 logs my-app
pm2 logs --lines 100
pm2 restart my-app
pm2 stop my-app
pm2 delete my-app
pm2 reload my-app
pm2 show my-app
pm2 monit
Auto-Start on Boot
pm2 save
pm2 startup
Next.js / Production Builds
npm run build
pm2 start npm --name "my-app" -- start
pm2 start ecosystem.config.js
Ecosystem File (ecosystem.config.js)
module.exports = {
apps: [{
name: 'my-app',
script: 'npm',
args: 'start',
cwd: '/path/to/app',
env: {
NODE_ENV: 'production',
PORT: 3000
}
}]
}
Useful Flags
| Flag | Description |
|---|
--name | Process name |
--watch | Restart on file changes |
-i max | Cluster mode (all CPUs) |
--max-memory-restart 200M | Auto-restart on memory limit |
--cron "0 * * * *" | Scheduled restart |
Cleanup
pm2 delete all
pm2 kill
pm2 unstartup