# 常用指令
TIP
- 安装:npm install pm2 -g
- pm2 --version
# 命令
pm2 start xx.js 或 配置文件
启动服务pm2 list
查看进程列表pm2 restart appName / id
启动服务或某个进程pm2 stop appName / id
停止服务或某个进程pm2 delete appName / id
删除服务或某个进程pm2 info appName / id
查看服务或某个进程的信息pm2 log appName / id
查看服务或某个进程的日志信息pm2 monit appName / id
查看服务或某个进程的监控信息,像 CPU 或内存的情况
# 测试文件 app.js
const http = require("http");
const server = http.createServer((req, res) => {
// 正常访问日志
console.log('Current time is ', Date now())
// 错误日志
console.error('假装出错了', Date.now())
res.setHeader('Content-type', 'application/json')
res.end(
JSON.stringify({
errno: 0,
msg: "pm2 start server",
})
);
});
server.listen(9999, () => {
console.log("Server is running at port 9999");
});
# pm2 start app.js
pm2 list
得到的大概也是这个样子的列表
[PM2] Starting /Users/hanzhizhen/code-study/pm2-test/app.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ app │ default │ 1.0.0 │ fork │ 25017 │ 0s │ 0 │ online │ 0% │ 2.5mb │ han… │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
# pm2 restart
重启是通过 pm2 restat id值
重启的,执行 pm2 restart 0
, 其中刷新符号 ↺ 的一列表示的是重启的次数
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [app.js](ids: [ 0 ])
[PM2] [app](0) ✓
┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ app │ default │ 1.0.0 │ fork │ 25067 │ 0s │ 1 │ online │ 0% │ 2.7mb │ han… │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
# pm2 info
Describing process with id 0 - name app
┌───────────────────┬───────────────────────────────────────────────────────────────────────────┐
│ status │ online │
│ name │ app │
│ namespace │ default │
│ version │ 1.0.0 │
│ restarts │ 1 │
│ uptime │ 56s │
│ script path │ /Users/hanzhizhen/code-study/pm2-test/app.js 脚本文件地址 │
│ script args │ N/A │
│ error log path │ /Users/hanzhizhen/.pm2/logs/app-error.log 错误日志文件,可配置 │
│ out log path │ /Users/hanzhizhen/.pm2/logs/app-out.log 访问日志文件,可配置 │
│ pid path │ /Users/hanzhizhen/.pm2/pids/app-0.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ /Users/hanzhizhen/code-study/pm2-test │
│ exec mode │ fork_mode │
│ node.js version │ 14.17.3 │
│ node env │ N/A │
│ watch & reload │ ✘ │
│ unstable restarts │ 0 │
│ created at │ 2022-02-05T09:25:04.930Z │
└───────────────────┴───────────────────────────────────────────────────────────────────────────┘
Actions available
┌────────────────────────┐
│ km:heapdump │
│ km:cpu:profiling:start │
│ km:cpu:profiling:stop │
│ km:heap:sampling:start │
│ km:heap:sampling:stop │
└────────────────────────┘
Trigger via: pm2 trigger app <action_name>
Code metrics value
┌────────────────────────┬──────────┐
│ Heap Size │ 6.32 MiB │
│ Heap Usage │ 82.76 % │
│ Used Heap Size │ 5.23 MiB │
│ Active requests │ 0 │
│ Active handles │ 4 │
│ Event Loop Latency │ 0.90 ms │
│ Event Loop Latency p95 │ 1.72 ms │
└────────────────────────┴──────────┘
Divergent env variables from local env
Add your own code metrics: http://bit.ly/code-metrics
Use `pm2 logs app [--lines 1000]` to display logs
Use `pm2 env 0` to display environment variables
Use `pm2 monit` to monitor CPU and Memory usage app
# pm2 log
大概就能看到 console.log
的日志信息
/Users/hanzhizhen/.pm2/logs/app-out.log last 15 lines:
0|app | Server is running at port 9999
0|app | Server is running at port 9999
0|app | Server is running at port 9999
0|app | Server is running at port 9999
0|app | Current time is 1644053687804
0|app | 假装出错了 1644053687804
0|app | Current time is 1644053688338
0|app | 假装出错了 1644053688338
0|app | Current time is 1644053691431
0|app | 假装出错了 1644053691431
0|app | Current time is 1644053691516
0|app | 假装出错了 1644053691516
# pm2 monit
┌─ Process List ────────────────────────────┐┌── app Logs ───────────────────────────────────────────────────────────────────────────────────────────┐
│[ 0] app Mem: 36 MB CPU: 0 % onl ││ app > Current time is 1644053836195 │
│ ││ app > 假装出错了 1644053836195 │
│ ││ app > Current time is 1644053836365 │
│ ││ app > 假装出错了 1644053836365 │
│ ││ app > Current time is 1644053837575 │
│ ││ │
└───────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ Custom Metrics ──────────────────────────┐┌─ Metadata ──────────────────────────────────────────────────────────────────────────────────────────────┐
│ Heap Size 6.57 MiB ││ App Name app │
│ Heap Usage 81.06 % ││ Namespace default │
│ Used Heap Size 5.33 MiB ││ Version 1.0.0 │
│ Active requests 0 ││ Restarts 1 │
│ Active handles 5 ││ Uptime 12m │
│ Event Loop Latency 0.88 ms ││ Script path /Users/hanzhizhen/code-study/pm2-test/app.js │
│ Event Loop Latency p95 1.91 ms ││ Script args N/A │
│ HTTP Mean Latency 1 ms ││ Interpreter node │
└───────────────────────────────────────────┘└─────────────────────────────────────────────────────────────────────────────────────────────────────────┘
left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit To go further check out https://pm2.io/
← 在 Node 中使用 JWT 守护进程 →