What is PM2?
PM2 (Process Manager 2) is a production-ready process manager for Node.js applications. Keep apps running 24/7 with automatic restarts, load balancing, and zero-downtime deployments.
โก Key Features
- Process Management & Auto Restart
- Load Balancing & Cluster Mode
- Monitoring & Log Management
- Zero Downtime Deployments
๐ฏ Use Cases
- Production Deployment
- Microservices Management
- Performance Optimization
- Real-time Monitoring
๐ฅ PM2 - Install & Version
Install PM2 globally and verify installation.
npm install -g pm2
pm2 -v
When to use
- First-time PM2 setup
- CI/CD agents
- VPS or production servers
๐ PM2 - Start Application
Start a Node.js application with a custom name.
pm2 start app.js --name my-app
Start with environment variables.
NODE_ENV=production pm2 start app.js --name my-app
๐ PM2 - Start with Port
Run application on a specific port.
PORT=3000 pm2 start app.js --name my-app
๐ PM2 - List Processes
View all running processes managed by PM2.
pm2 list
โน๏ธ PM2 - Process Status & Info
Get detailed information about a specific process.
pm2 show my-app
Includes:
- PID
- Memory usage
- Restart count
- Environment variables
๐ PM2 - Stop Application
Stop a running process.
pm2 stop my-app
Stop by process ID.
pm2 stop 0
๐ PM2 - Restart Application
Restart application without downtime (single instance).
pm2 restart my-app
Restart all applications.
pm2 restart all
โก PM2 - Reload (Zero Downtime)
Reload application with zero downtime (cluster mode).
pm2 reload my-app
Recommended for
- Production deployments
- API servers
- User-facing services
๐๏ธ PM2 - Delete Process
Remove application from PM2.
pm2 delete my-app
Delete all processes.
pm2 delete all
๐ PM2 - Logs (All Processes)
View combined logs.
pm2 logs
๐ PM2 - Logs (Single App)
View logs for a specific app.
pm2 logs my-app
๐งน PM2 - Clear Logs
Clear all PM2 logs.
pm2 flush
Useful when
- Disk usage increases
- Logs grow too large
๐ PM2 - Monitor Resources
Live monitoring dashboard.
pm2 monit
Shows:
- CPU usage
- Memory usage
- Restart count
๐ PM2 - Cluster Mode
Run application in cluster mode using all CPU cores.
pm2 start app.js -i max --name my-app
Run with specific number of instances.
pm2 start app.js -i 4 --name my-app
๐ฆ PM2 - Ecosystem File
Generate ecosystem configuration.
pm2 ecosystem
Start using ecosystem file.
pm2 start ecosystem.config.js
๐ PM2 - Ecosystem Example
module.exports = {
apps: [
{
name: "my-app",
script: "app.js",
instances: "max",
exec_mode: "cluster",
env: {
NODE_ENV: "development"
},
env_production: {
NODE_ENV: "production"
}
}
]
}
๐ PM2 - Start with Environment
Start application using production environment.
pm2 start ecosystem.config.js --env production
๐ PM2 - Auto Restart on Crash
PM2 automatically restarts crashed applications.
To limit restarts:
pm2 start app.js --max-restarts 5
๐พ PM2 - Save Process List
Save current process list for startup.
pm2 save
๐ PM2 - Startup on Server Boot
Generate startup script.
pm2 startup
Follow the command output and then run:
pm2 save
๐ PM2 - Reload on File Change (Dev)
Watch files and restart on change.
pm2 start app.js --watch
Not recommended for production
๐ง PM2 - Update Environment Variables
Reload app after updating env variables.
pm2 restart my-app --update-env
๐ PM2 - Reset Restart Counter
Reset restart count for an app.
pm2 reset my-app
โ PM2 - Uninstall PM2
Remove PM2 globally.
npm uninstall -g pm2
โ PM2 - Common Production Checklist
- Use cluster mode
- Enable startup script
- Save process list
- Use ecosystem file
- Monitor memory usage
- Rotate logs externally
- Reload instead of restart