nodejs pm2

最後更新: 2024-03-08

目錄

  • Installation
  • CLI
    Start an app
    Stop an app
    Delete an app
    Reload & Restart
    ping pong
    Listing App Proccess
    Display all informations about a specific process
    Log
    Dashboard
  • Auto Start
  • Restart strategies

介紹

Process manager for Node.JS

  • Built-in load balancer (HTTP/TCP/UDP queries)
  • Reload app without downtime (Cluster Mode)
  • To facilitate common system admin tasks

HomePage

 


Installation

 

su program_user

cd ~                      # 它會在當前目錄建立 "node_modules/.bin"

npm install pm2

修改 .bashrc 加入

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/node_modules/.bin
export PATH

Test:

su program_user

pm2 ls

 


CLI

 

 

Start an app

pm2 start app.js

# Specify an app name

--name <app_name>

# Watch and Restart app when files change

--watch

# Set memory threshold for app reload

--max-memory-restart <200MB>

# Specify log file

--log <log_path>

Cluster Mode

# processes = 'max' / -1 (all cpu minus 1) / N

pm2 start api.js -i <processes>

json file

module.exports = {
  apps : [{
    script    : "api.js",
    instances : "max",
    exec_mode : "cluster"
  }]
}

Stop an app

pm2 stop app_name

Delete an app

pm2 delete app_name

Reload & Restart

pm2 reload app_name

pm2 reload all

pm2 restart app_name

reload vs restart

reload = Hot Reload. In cluster mode reload without any downtime.

ping pong

pm2 ping

{ msg: 'pong' }

Listing App Proccess

pm2 ls     # list=ls=status

┌─────┬──────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name             │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 1   │ app              │ default     │ 1.0.0   │ cluster │ 12265    │ 18h    │ 9    │ online    │ 0%       │ 264.8mb  │ root     │ disabled │
│ 2   │ app              │ default     │ 1.0.0   │ cluster │ 12279    │ 18h    │ 9    │ online    │ 0%       │ 251.3mb  │ root     │ disabled │
└─────┴──────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Module
┌────┬──────────────────────────────┬───────────────┬──────────┬──────────┬──────┬──────────┬──────────┬──────────┐
│ id │ module                       │ version       │ pid      │ status   │ ↺    │ cpu      │ mem      │ user     │
├────┼──────────────────────────────┼───────────────┼──────────┼──────────┼──────┼──────────┼──────────┼──────────┤
│ 0  │ pm2-logrotate                │ 2.7.0         │ 1269     │ online   │ 0    │ 0%       │ 43.1mb   │ root     │
└────┴──────────────────────────────┴───────────────┴──────────┴──────────┴──────┴──────────┴──────────┴──────────┘

Display all informations about a specific process

pm2 describe ID

i.e.

pm2 describe 1

┌─────────────────────────────────────────────────────
│ status            │ online                         │
│ name              │ app                            │
│ namespace         │ default                        │
│ version           │ 1.0.0                          │
│ restarts          │ 9                              │
│ uptime            │ 18h                            │
│ script path       │ /data/web/backend/app/index.js │
│ script args       │                                │
│ error log path    │ /root/.pm2/logs/app-error.log  │
│ out log path      │ /root/.pm2/logs/app-out.log    │
│ pid path          │ /root/.pm2/pids/app-1.pid      │
│ interpreter       │ node                           │
│ interpreter args  │ N/A                            │
│ script id         │ 1                              │
│ exec cwd          │ /data/web/backend/app          │
│ exec mode         │ cluster_mode                   │
│ node.js version   │ 16.6.2                         │
│ node env          │ N/A                            │
│ watch & reload    │ ✘                              │
│ unstable restarts │ 0                              │
│ created at        │ 2022-01-18T09:23:25.181Z       │
└─────────────────────────────────────────────────────
 Revision control metadata
 ...
 Actions available
 ...
 Code metrics value
 ...
 Divergent env variables from local env
 ...

Log

# 在 Terminal 不斷 output 所有 App 的 log

pm2 logs

# 其中某 app 的 log

pm2 logs app

# 最後 200 行 log

pm2 logs --lines 200

Terminal Based Dashboard

pm2 monit

 


Auto Start

 

# Generating a Startup Script
# PM2 will automatically restart at boot

pm2 startup

# Saving the app list to be restored at reboot

pm2 save

# Manually resurrect processes

pm2 resurrect

# Disabling startup system

pm2 unstartup

 


Restart strategies

 

Restart at cron time

pm2 start app.js --cron-restart="0 0 * * *"

pm2 restart app --cron-restart="0 0 * * *"

To disable cron restart

pm2 restart app --cron-restart 0

Restart on file change

pm2 start app.js --watch

Memory based restart strategy

# PM2 internal worker (which checks memory), starts every 30 seconds

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