PM2 Cheatsheet

Cheatsheet DevOps

Production-ready PM2 commands and patterns for Node.js process management

What is this? Official Docs

Production process manager for Node.js that keeps applications alive, enables load balancing, and provides monitoring capabilities.

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