>_cat pm2.md|Web & App Servers
PM2 Cheatsheet
SPEC: MDX·/pm2·HTTP reverse proxies, application runtimes, and log analysis.
PM2 is a daemon process manager designed for production Node.js applications that keeps apps alive forever, reloads them with zero downtime, and simplifies common system administration tasks.
Installation
# Global install via NPM
npm install pm2@latest -g
# Or via Yarn
yarn global add pm2
# Check version
pm2 -vStarting Applications
Basic Start
pm2 start app.js # Start script with default name
pm2 start app.js --name "api" # Start and assign custom process name
pm2 start npm --name "web" -- start # Run npm script (e.g. Next.js / Nuxt)
pm2 start "npm run dev" --name "dev-app"Cluster Mode (Scale Across CPU Cores)
pm2 start app.js -i max # Spread across all available CPU cores
pm2 start app.js -i 4 # Start exactly 4 instances in cluster mode
pm2 scale api 6 # Scale running app to 6 instancesWatch & Auto Restart
pm2 start app.js --watch # Restart on file changes
pm2 start app.js --max-memory-restart 500M # Auto-restart if RAM exceeds 500MBProcess Management
pm2 stop app_name # Stop process by name
pm2 stop all # Stop all running processes
pm2 restart app_name # Hard restart process
pm2 restart all # Restart all processes
pm2 reload app_name # Zero-downtime hot reload (cluster mode)
pm2 reload all # Zero-downtime reload all processes
pm2 delete app_name # Stop and remove from PM2 list
pm2 delete all # Delete all registered processesMonitoring & Inspection
pm2 list # Display all managed processes and status
pm2 ls # Short alias for list
pm2 status # Table view of CPU/RAM/Uptime
pm2 monit # Real-time terminal dashboard (CPU, memory, logs)
pm2 show app_name # Inspect environment, paths, metadata of app
pm2 env 0 # Show exported environment variables for process ID 0Logs Management
pm2 logs # Tail logs across all apps
pm2 logs app_name # Tail logs for specific app
pm2 logs --lines 100 # Output last 100 log lines
pm2 logs --raw # Stream logs without timestamps/metadata
pm2 flush # Truncate and clear all log files
pm2 reloadLogs # Reopen log files (useful with logrotate)System Startup & Persistence
Save current processes and configure auto-start on server reboot:
# 1. Save currently running process list
pm2 save
# 2. Generate and configure system startup script
pm2 startup
# (Run the generated sudo env command printed by PM2)
# 3. Disable startup script
pm2 unstartup
# 4. Restore previously saved processes
pm2 resurrectEcosystem File (Configuration File)
Generate Ecosystem File
pm2 init # Creates ecosystem.config.jsSample ecosystem.config.js
module.exports = {
apps: [
{
name: "api-service",
script: "./server.js",
instances: "max",
exec_mode: "cluster",
autorestart: true,
max_memory_restart: "500M",
env: {
NODE_ENV: "development",
PORT: 3000
},
env_production: {
NODE_ENV: "production",
PORT: 8080
}
}
]
};Run with Ecosystem File
pm2 start ecosystem.config.js
pm2 start ecosystem.config.js --env production
pm2 reload ecosystem.config.jsQuick Reference
| Command | Description |
|---|---|
pm2 list | View active processes |
pm2 logs | Stream consolidated log feed |
pm2 reload all | Zero-downtime rolling reload |
pm2 save | Persist process list to disk |
pm2 monit | Open terminal visual monitor |
pm2 flush | Empty all process log files |