# curl https://devtools.krishanchawla.com/raw/pm2-cheatsheet.txt PM2 CHEATSHEET Production-ready PM2 commands and patterns for Node.js process management Category: DevOps · Type: cheatsheet Official docs: https://pm2.keymetrics.io/ ──────────────────────────────────────────────────────────── ## 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. ```bash 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. ```bash pm2 start app.js --name my-app ``` Start with environment variables. ```bash NODE_ENV=production pm2 start app.js --name my-app ``` ## 🔌 PM2 - Start with Port Run application on a specific port. ```bash PORT=3000 pm2 start app.js --name my-app ``` ## 📊 PM2 - List Processes View all running processes managed by PM2. ```bash pm2 list ``` ## ℹ️ PM2 - Process Status & Info Get detailed information about a specific process. ```bash pm2 show my-app ``` Includes: - PID - Memory usage - Restart count - Environment variables ## 🛑 PM2 - Stop Application Stop a running process. ```bash pm2 stop my-app ``` Stop by process ID. ```bash pm2 stop 0 ``` ## 🔄 PM2 - Restart Application Restart application without downtime (single instance). ```bash pm2 restart my-app ``` Restart all applications. ```bash pm2 restart all ``` ## ⚡ PM2 - Reload (Zero Downtime) Reload application with zero downtime (cluster mode). ```bash pm2 reload my-app ``` **Recommended for** - Production deployments - API servers - User-facing services ## 🗑️ PM2 - Delete Process Remove application from PM2. ```bash pm2 delete my-app ``` Delete all processes. ```bash pm2 delete all ``` ## 📄 PM2 - Logs (All Processes) View combined logs. ```bash pm2 logs ``` ## 📝 PM2 - Logs (Single App) View logs for a specific app. ```bash pm2 logs my-app ``` ## 🧹 PM2 - Clear Logs Clear all PM2 logs. ```bash pm2 flush ``` **Useful when** - Disk usage increases - Logs grow too large ## 📈 PM2 - Monitor Resources Live monitoring dashboard. ```bash pm2 monit ``` Shows: - CPU usage - Memory usage - Restart count ## 🔀 PM2 - Cluster Mode Run application in cluster mode using all CPU cores. ```bash pm2 start app.js -i max --name my-app ``` Run with specific number of instances. ```bash pm2 start app.js -i 4 --name my-app ``` ## 📦 PM2 - Ecosystem File Generate ecosystem configuration. ```bash pm2 ecosystem ``` Start using ecosystem file. ```bash pm2 start ecosystem.config.js ``` ## 📋 PM2 - Ecosystem Example ```js 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. ```bash pm2 start ecosystem.config.js --env production ``` ## 🔁 PM2 - Auto Restart on Crash PM2 automatically restarts crashed applications. To limit restarts: ```bash pm2 start app.js --max-restarts 5 ``` ## 💾 PM2 - Save Process List Save current process list for startup. ```bash pm2 save ``` ## 🚀 PM2 - Startup on Server Boot Generate startup script. ```bash pm2 startup ``` Follow the command output and then run: ```bash pm2 save ``` ## 👀 PM2 - Reload on File Change (Dev) Watch files and restart on change. ```bash pm2 start app.js --watch ``` **Not recommended for production** ## 🔧 PM2 - Update Environment Variables Reload app after updating env variables. ```bash pm2 restart my-app --update-env ``` ## 🔄 PM2 - Reset Restart Counter Reset restart count for an app. ```bash pm2 reset my-app ``` ## ❌ PM2 - Uninstall PM2 Remove PM2 globally. ```bash 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 ──────────────────────────────────────────────────────────── Full page: https://devtools.krishanchawla.com/devtools/pm2-cheatsheet More tools: https://devtools.krishanchawla.com