Catalog / PM2 Cheatsheet

PM2 Cheatsheet

A comprehensive cheat sheet for PM2, a process manager for Node.js applications. Covers essential commands for managing, monitoring, and deploying applications with PM2.

PM2 Basics

Starting Applications

pm2 start app.js

Start a Node.js application.

Example:
pm2 start my_app.js

pm2 start app.js --name my-api

Start an application with a specific name.

Example:
pm2 start worker.js --name background-tasks

pm2 start app.js -i max

Start an application in cluster mode, utilizing all available CPU cores.

Example:
pm2 start api.js -i max

pm2 start app.js -i 4

Start an application in cluster mode with a specified number of instances.

Example:
pm2 start worker.js -i 2

pm2 start npm -- start

Start an application using npm.

Example:
pm2 start npm -- run production

pm2 start app.js --watch

Automatically restart the app when files change.

Example:
pm2 start index.js --watch

pm2 start app.js --ignore-watch="["node_modules", "test"]"

Ignore specific paths when watching for file changes.

Example:
pm2 start app.js --watch --ignore-watch="["node_modules", "logs", "tmp"]"

pm2 start ecosystem.config.js

Start applications defined in an ecosystem file.

Example:
pm2 start ecosystem.config.js

Managing Processes

pm2 stop <app_name|id>

Stop a specific process by name or ID.

Example:
pm2 stop my-api
pm2 stop 0

pm2 restart <app_name|id>

Restart a specific process by name or ID.

Example:
pm2 restart worker
pm2 restart 1

pm2 delete <app_name|id>

Delete a specific process by name or ID.

Example:
pm2 delete my-api
pm2 delete 0

pm2 stop all

Stop all running processes.

Example:
pm2 stop all

pm2 restart all

Restart all running processes.

Example:
pm2 restart all

pm2 delete all

Delete all processes from PM2.

Example:
pm2 delete all

pm2 reload all

Reload all processes, minimizing downtime.

Example:
pm2 reload all

Monitoring and Logs

pm2 list

List all processes managed by PM2.

Example:
pm2 list

pm2 show <app_name|id>

Show detailed information about a specific process.

Example:
pm2 show my-api
pm2 show 0

pm2 monit

Open a real-time process monitoring dashboard.

Example:
pm2 monit

pm2 logs <app_name|id>

Display logs for a specific process.

Example:
pm2 logs my-api
pm2 logs 0

pm2 logs

Show aggregated logs for all processes.

Example:
pm2 logs

pm2 flush

Clear all PM2 log files.

Example:
pm2 flush

pm2 reloadLogs

Reload all PM2 logs.

Example:
pm2 reloadLogs

Ecosystem Configuration

Ecosystem File Structure

An ecosystem.config.js or ecosystem.config.json file defines application configurations for PM2.

Example ecosystem.config.js:

module.exports = {
  apps : [{
    name   : 'my-api',
    script : 'api.js',
    instances : 'max',
    autorestart : true,
    watch : false,
    max_memory_restart : '1G',
    env : {
      NODE_ENV : 'development'
    },
    env_production : {
      NODE_ENV : 'production'
    }
  }]
};

Common Ecosystem Options

name

Name of the application.

Example:
name: 'my-app'

script

Path to the application script.

Example:
script: 'index.js'

instances

Number of application instances. Use max for maximum available cores.

Example:
instances: 'max'

autorestart

Automatically restart the application if it crashes.

Example:
autorestart: true

watch

Enable file watching for automatic restarts on code changes.

Example:
watch: true

max_memory_restart

Maximum memory usage before restarting the application.

Example:
max_memory_restart: '1G'

env

Environment variables for all environments.

Example:
env: { NODE_ENV: 'development' }

env_<environment>

Environment-specific variables (e.g., env_production).

Example:
env_production: { NODE_ENV: 'production', PORT: 80 }

Deploying with Ecosystem

pm2 deploy ecosystem.config.js production setup

Setup the production environment.

Example:
pm2 deploy ecosystem.config.js production setup

pm2 deploy ecosystem.config.js production update

Update the production environment with new code.

Example:
pm2 deploy ecosystem.config.js production update

pm2 deploy ecosystem.config.js production

Deploy the application to the production environment.

Example:
pm2 deploy ecosystem.config.js production

pm2 deploy ecosystem.config.js production revert <n>

Revert to a previous deployment (n = number of revisions back).

Example:
pm2 deploy ecosystem.config.js production revert 1

pm2 ecosystem

Generate a sample ecosystem.config.js file.

Example:
pm2 ecosystem

Advanced PM2 Usage

Startup Management

pm2 startup

Generate a startup script to automatically start PM2 and managed processes on system boot.

Example:
pm2 startup

pm2 save

Save the current process list to be restored on system boot.

Example:
pm2 save

pm2 unstartup

Disable PM2 from starting on boot (remove the startup script).

Example:
pm2 unstartup

pm2 resurrect

Restore previously saved processes.

Example:
pm2 resurrect

Process Interaction

pm2 sendSignal <signal> <app_name|id>

Send a specific signal to a process.

Example:
pm2 sendSignal SIGUSR2 my-app

pm2 trigger <event_name> <app_name|id>

Trigger a custom event in a PM2 managed process, if the application is set up to listen for the event.

Example:
pm2 trigger reload my-app

pm2 ping

Check if the PM2 daemon is running.

Example:
pm2 ping

pm2 pull

Pull new updates.

Example:
pm2 pull

Module Management

pm2 install <module_name>

Install a PM2 module.

Example:
pm2 install pm2-logrotate

pm2 uninstall <module_name>

Uninstall a PM2 module.

Example:
pm2 uninstall pm2-logrotate

pm2 list

Will also display modules list.

Example:
pm2 list

Troubleshooting and Tips

Common Issues

If an application fails to start, check the PM2 logs for error messages using pm2 logs <app_name|id>. Ensure that all dependencies are installed (npm install) and that the application code is free of errors.

If the application is not automatically restarting after a crash, check the autorestart setting in the ecosystem file or command line arguments. Also, ensure that the max_memory_restart setting is appropriate for your application’s memory usage.

Useful Tips

Use an ecosystem file to manage application configurations. It provides a centralized way to define application settings, environment variables, and deployment options.

Utilize PM2 modules for extended functionality. For example, pm2-logrotate can be used to automatically rotate log files and prevent them from growing too large.

Monitor application performance using pm2 monit to identify and address issues such as high CPU usage or memory leaks.

Configure environment-specific variables in the ecosystem file to easily switch between development, staging, and production environments.

Updating PM2

pm2 update

Update PM2 to the latest version, ensuring the daemon and all managed processes are updated.

Example:
pm2 update

pm2 show pm2

Check the current PM2 version.

Example:
pm2 show pm2