最後更新: 2022-03-30
介紹
Process manager for Node.JS
https://pm2.keymetrics.io/
Install
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>
Stop
pm2 stop app_name
Reload & Restart
pm2 reload app_name
pm2 reload all
pm2 restart app_name
Ensure pm2 daemon has been launched
pm2 ping
{ msg: 'pong' }
Listing 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
pm2 logs # 不斷 update 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