Post

pm2

PM2 Cheat Sheet

Installation

1
2
3
4
5
npm install -g pm2           # Install PM2 globally
pm2 update                   # Update PM2 to latest version
pm2 -v                       # Check PM2 version
node -v                       # Check Node.js version
npm -v                        # Check npm version

Starting Applications

1
2
3
4
5
6
7
8
9
10
11
12
13
14
pm2 start app.js                          # Start Node.js app
pm2 start app.js --name my-app            # Name the process
pm2 start app.js --watch                  # Auto-restart on file changes
pm2 start app.js -i max                   # Cluster mode (all CPUs)
pm2 start app.js -i 4                     # Cluster with 4 instances

pm2 start "serve -s dist -l 3000"        # Start static server / shell command
pm2 start script.sh                       # Start bash script
pm2 start app.py --interpreter python3    # Python app
pm2 start ecosystem.config.js             # Start using PM2 config

pm2 start app.js --time                  # Show timestamp in logs
pm2 start app.js --no-autorestart        # Disable auto-restart
pm2 start app.js --max-memory-restart 200M   # Restart if memory exceeds

Serve a static folder with PM2 built-in server

1
2
3
4
5
6
pm2 serve dist 3000 --name my-app          # Serve 'dist' on port 3000
pm2 serve dist $PORT --name $NAME         # Using environment variables
pm2 serve dist 3000 --spa                  # Single Page App mode (rewrite all to index.html)
pm2 serve dist 3000 --spa --name my-app   # SPA mode with custom name
pm2 save                                  # Save process for auto-start on boot
pm2 startup                               # Enable PM2 on system boot

Stopping & Deleting

1
2
3
4
5
6
7
8
9
pm2 stop all                # Stop all processes
pm2 stop my-app             # Stop by name
pm2 stop 0                  # Stop by ID

pm2 delete all              # Delete all processes
pm2 delete my-app           # Delete by name
pm2 delete 0                # Delete by ID

pm2 kill                    # Kill PM2 daemon and all processes

Restarting & Reloading

1
2
3
4
5
6
7
pm2 restart all             # Restart all processes
pm2 restart my-app          # Restart by name
pm2 restart 0               # Restart by ID

pm2 reload all              # Zero-downtime reload (cluster mode)
pm2 reload my-app           # Zero-downtime reload by name
pm2 reload 0                # Reload by ID

Monitoring & Status

1
2
3
4
5
6
pm2 list                    # List all processes
pm2 status                  # Same as list
pm2 show my-app             # Detailed info about a process
pm2 describe my-app         # Same as show
pm2 monit                   # Real-time monitoring dashboard
pm2 top                     # CPU/Memory usage dashboard

Logs

1
2
3
4
5
6
7
8
pm2 logs                        # Show all logs
pm2 logs my-app                 # Show specific app logs
pm2 logs my-app --lines 100     # Show last 100 lines
pm2 logs --nostream             # Print logs once, no streaming
pm2 flush                       # Clear all logs
pm2 reloadLogs                  # Reload log files
pm2 reset my-app                # Reset restart count & logs
pm2 logrotate:enable            # Enable automatic log rotation

Log Rotation Config Example:

1
2
3
4
5
pm2 install pm2-logrotate
pm2 set pm2-logrotate:max_size 10M
pm2 set pm2-logrotate:retain 30
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'   # Daily

Startup & Auto-restart on Boot

1
2
3
4
5
pm2 startup                     # Generate startup script
pm2 startup ubuntu              # Generate for specific OS
pm2 save                        # Save current process list
pm2 resurrect                   # Restore saved process list
pm2 unstartup                   # Disable startup script

Cluster Mode

1
2
3
4
5
6
pm2 start app.js -i 0           # Auto-detect CPU count
pm2 start app.js -i max         # Use all available CPUs
pm2 start app.js -i 4           # Use 4 CPUs
pm2 scale my-app 4              # Scale to 4 instances
pm2 scale my-app +2             # Add 2 more instances
pm2 scale my-app -1             # Remove 1 instance

Environment Variables

1
2
3
pm2 start app.js --env production           # Use production env
pm2 restart my-app --env production         # Restart with env
pm2 start app.js --env development          # Use development env

Example with ecosystem file:

1
2
env: { NODE_ENV: 'development' },
env_production: { NODE_ENV: 'production' }

Ecosystem Config File

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// ecosystem.config.js
module.exports = {
  apps: [
    {
      name: 'my-app',
      script: 'app.js',
      instances: 'max',
      exec_mode: 'cluster',
      watch: true,
      max_memory_restart: '200M',
      env: {
        NODE_ENV: 'development',
        PORT: 3000
      },
      env_production: {
        NODE_ENV: 'production',
        PORT: 8080
      }
    }
  ],
  deploy: {
    production: {
      user: 'ubuntu',
      host: 'server.com',
      ref: 'origin/main',
      repo: 'git@github.com:user/repo.git',
      path: '/var/www/my-app',
      'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production'
    }
  }
}
1
2
3
pm2 start ecosystem.config.js               # Start all apps
pm2 start ecosystem.config.js --env production
pm2 reload ecosystem.config.js --env production

Useful One-liners

1
2
3
4
5
6
pm2 list --no-color             # For CI/logs
pm2 pid my-app                  # Get PID
pm2 sendSignal SIGUSR2 my-app   # Send signal
pm2 reset my-app                # Reset restart count & logs
pm2 info my-app                 # Full process info
pm2 prettylist                  # Pretty JSON of process info

For Expo / Static Web Apps

1
2
3
4
5
6
7
8
9
# Install serve
npm install -g serve

# Start Expo dist folder
pm2 start "serve -s /path/to/dist -l 3000" --name my-expo-app

# Save and enable on boot
pm2 save
pm2 startup

Advanced PM2 Features

Watch & Auto-Restart

1
pm2 start app.js --watch --ignore-watch="node_modules logs"

Memory & CPU Limit Auto-Restart

pm2 start app.js --max-memory-restart 300M

Custom Script Arguments

1
pm2 start app.js -- --port 3000 --verbose

Sending Signals to Apps

1
2
pm2 sendSignal SIGINT my-app
pm2 sendSignal SIGHUP my-app

Debugging

1
2
pm2 logs my-app --lines 200
pm2 show my-app

Quick Reference

Action Command
Start app pm2 start app.js --name my-app
Stop app pm2 stop my-app
Restart app pm2 restart my-app
Zero-downtime reload pm2 reload my-app
View logs pm2 logs my-app
Monitor pm2 monit
Save process list pm2 save
Enable on boot pm2 startup
Delete app pm2 delete my-app
Scale pm2 scale my-app +2
Watch files pm2 start app.js --watch
Memory restart pm2 start app.js --max-memory-restart 200M
PID pm2 pid my-app
This post is licensed under CC BY 4.0 by the author.