Published on October 14, 2024By DeveloperBreeze

PM2 Cheatsheet

PM2 Installation

npm install pm2@latest -g

Process Management

Start an application

pm2 start app.js
  • Start your app as a process managed by PM2.

Start an app with a name

pm2 start app.js --name "app-name"

Start an app on a specific port

pm2 start app.js --name "app-name" -- 3000

Start an app in cluster mode (multi-core support)

pm2 start app.js -i max
  • -i max starts as many instances as there are CPU cores.

Start app and watch for changes (useful for development)

pm2 start app.js --watch
  • Automatically restarts the app when changes are detected.

Stop an application

pm2 stop <app_name_or_id>

Restart an application

pm2 restart <app_name_or_id>

Delete an application

pm2 delete <app_name_or_id>

List running applications

pm2 list

Show details of a specific process

pm2 show <app_name_or_id>

Reload all applications (Zero Downtime Reload)

pm2 reload all

Start all previously stopped applications

pm2 start all

Logs and Monitoring

View logs of all apps

pm2 logs

View logs of a specific app

pm2 logs <app_name_or_id>

View real-time CPU/Memory usage of all apps

pm2 monit

Flush logs (clear logs for all apps)

pm2 flush

Reload logs (without restarting apps)

pm2 reloadLogs

Environment Variables

Start app with environment variables

pm2 start app.js --env production
  • The environment variables are defined in ecosystem.config.js or can be provided inline.

Start app with inline environment variables

pm2 start app.js --env VAR=value

Configuration Files

Use `ecosystem.config.js` for configuration (recommended)

  1. Create ecosystem.config.js:
   module.exports = {
     apps: [
       {
         name: 'app-name',
         script: 'app.js',
         instances: 'max',
         exec_mode: 'cluster',
         env: {
           NODE_ENV: 'development',
         },
         env_production: {
           NODE_ENV: 'production',
         }
       }
     ]
   };
  1. Start all apps in the config:
   pm2 start ecosystem.config.js
  1. Start with a specific environment:
   pm2 start ecosystem.config.js --env production

Startup and Auto-Restart

Generate startup script (persistent after reboot)

pm2 startup
  • Follow the instructions provided to enable PM2 on system startup.

Save the current process list

pm2 save
  • Save the list of running apps for auto-respawn after system restart.

Resurrection (restore apps after system reboot)

pm2 resurrect

Reset all processes

pm2 reset

Process Management for Microservices

Scale app horizontally

pm2 scale <app_name_or_id> <number_of_instances>

Limit memory usage for an app

pm2 start app.js --max-memory-restart 300M
  • Auto-restart when memory usage exceeds the limit.

Monitoring and Logs Management

Inspect logs for CPU or memory overuse

pm2 logs --lines 100
  • Shows the last 100 lines of logs for all apps.

Enable detailed log rotation (automatically rotate logs)

  1. Install the module:
   pm2 install pm2-logrotate
  1. Configure the log rotation:
   pm2 set pm2-logrotate:max_size 10M
   pm2 set pm2-logrotate:retain 30

Reloading and Zero Downtime

Zero downtime reload for production apps

pm2 reload <app_name_or_id>

Reload all apps with zero downtime

pm2 reload all

Miscellaneous Commands

Kill all processes managed by PM2

pm2 kill

Uninstall PM2 log rotation module

pm2 uninstall pm2-logrotate

Display memory and CPU usage

pm2 describe <app_name_or_id>

Get app metadata

pm2 env <app_name_or_id>

Cloning & Running App Across Multiple Servers

Run a command across all machines

pm2 deploy ecosystem.config.js production exec "npm install"

Update the app on all servers

pm2 deploy ecosystem.config.js production update

Deploy the app (via SSH)

  1. Define ecosystem.config.js:
   module.exports = {
     apps: [
       {
         name: 'app-name',
         script: 'app.js',
         env: {
           NODE_ENV: 'development',
         },
         env_production: {
           NODE_ENV: 'production',
         }
       }
     ],
     deploy: {
       production: {
         user: 'username',
         host: 'your-server.com',
         ref: 'origin/master',
         repo: 'git@github.com:your-repo.git',
         path: '/var/www/your-app',
         'post-deploy': 'npm install && pm2 reload ecosystem.config.js --env production',
       }
     }
   };
  1. Deploy your app:
   pm2 deploy ecosystem.config.js production
  1. Rollback:
   pm2 deploy ecosystem.config.js production revert 1

PM2 Health Checks

Check if PM2 is running

pm2 ping

Check version of PM2

pm2 -v

Comments

Please log in to leave a comment.