The introduction

PM2 is a daemon manager that manages and keeps applications online.


Function of PM2

Fork mode vs. Cluster mode

  • The fork model

    A single instance, can be modifiedexec_interpreterConfiguration item that uses PM2 to run services in PHP, Python and other languages.

    Pm2 uses Node by default.pm2 start app.jsIs equivalent torequire('child_process').spawn('node', ['server.js']).
  • Cluster mode multi-instance (only for NodeJS process), zero configuration implements load balancing among each instance.

Watch mode

After the file is modified, the PM2 automatically restarts the application.


Principle of PM2

  • Reference through the source code analysis node.js cluster module in the main functions to achieve PM2 source analysis

To be perfect


Common commands

Fork mode

  • pm2 start app.js –name my-api

    start and name process

Cluster mode

  • pm2 start app.js -i 0|max

    Start maximum processes with LB depending on available CPUs
  • pm2 scale app +3

    Scales app up by 3 workers
  • pm2 scale app 2

    Scales app up or down to 2 workers total

Listing

  • pm2 [list|ls|status]

    List the status of all application managed by PM2
  • pm2 jlist

    Print process list in raw JSON
  • pm2 prettylist

    Print process list in beautified JSON
  • pm2 describe 0

    Display all informations about a specific process
  • pm2 monit

    Terminal Based Dashboard

Logs

  • pm2 logs [–raw]

    Display all processes logs in streaming
  • pm2 logs –lines 200

    Dig in older logs
  • pm2 flush

    Display all processes logs in streaming
  • pm2 reloadLogs

    Reload all logs

Actions

  • pm2 stop all|id

    Stop all processes or specific process id
  • pm2 restart all|id

    Restart all processes or specific process id
  • pm2 reload all|id

    Will 0s downtime reload (for NETWORKED apps)
  • pm2 delete all|id

    Will remove all processes or specific process id from pm2 list

Misc

  • pm2 reset all|id

    Reset meta data (restarted time…)
  • pm2 updatePM2

    Update in memory pm2
  • pm2 ping

    Ensure pm2 daemon has been launched
  • pm2 sendSignal SIGUSR2 my-app

    Send system signal to script
  • pm2 start app.js –no-daemon
  • pm2 start app.js –no-vizion
  • pm2 start app.js –no-autorestart