# 守护进程

pm2 和 普通的 node 脚本 或者 nodemon 脚本 启动方式的区别是,后者启动脚本后碰到报错服务就挂掉了。

# 测试代码 app.js










 
 
 
 














const http = require("http");

const server = http.createServer((req, res) => {
  // 正常访问日志
  console.log("Current time is ", Date.now());

  // 错误日志
  console.error("假装出错了", Date.now());

  // 访问 error 服务会挂掉
  if (req.url === "/error") {
    throw new Error("报错了");
  }

  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");
});

# yarn dev 启动

yarn dev 通过 node 或者 nodemon 启动后,访问 /error 后,基本服务就酱紫了,挂了。想访问只能再重启一次

$ node app.js
Server is running at port 9999
Current time is  1644055273760
假装出错了 1644055273762
/Users/hanzhizhen/code-study/pm2-test/app.js:11
    throw new Error("报错了");
    ^

Error: 报错了
    at Server.<anonymous> (/Users/hanzhizhen/code-study/pm2-test/app.js:11:11)
    at Server.emit (events.js:375:28)
    at parserOnIncoming (_http_server.js:897:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

# pm2 start 启动

先看启动后的列表如下:

[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    │ 28514    │ 0s     │ 0    │ online    │ 0%       │ 2.2mb    │ han… │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

然后访问 /error 后,服务必然也是挂了。但是再访问首页仍然可以访问,这就是进程守护,我理解的就是服务进程挂了,能自动启动的效果。

看一下访问过 /error 页面后的 pm2 list情况,会发现 有重启的情况,这就是服务挂掉后自动重启的表征,号称进程守护

┌─────┬────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name   │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ app    │ default     │ 1.0.0   │ fork    │ 28610    │ 7s     │ 3    │ online    │ 0%       │ 38.4mb   │ han… │ disabled │
└─────┴────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

同时,pm2 log 能看到报错的信息

/Users/hanzhizhen/.pm2/logs/app-error.log last 15 lines:
0|app      | Error: 报错了
0|app      |     at Server.<anonymous> (/Users/hanzhizhen/code-study/pm2-test/app.js:11:11)
0|app      |     at Server.emit (events.js:375:28)
0|app      |     at Server.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/@pm2/io/build/main/metrics/httpMetrics.js:166:33)
0|app      |     at parserOnIncoming (_http_server.js:897:12)
0|app      |     at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
0|app      | 假装出错了 1644056274930
0|app      | Error: 报错了
0|app      |     at Server.<anonymous> (/Users/hanzhizhen/code-study/pm2-test/app.js:11:11)
0|app      |     at Server.emit (events.js:375:28)
0|app      |     at Server.<anonymous> (/usr/local/lib/node_modules/pm2/node_modules/@pm2/io/build/main/metrics/httpMetrics.js:166:33)
0|app      |     at parserOnIncoming (_http_server.js:897:12)
0|app      |     at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
0|app      | 假装出错了 1644056277961
0|app      | 假装出错了 1644056278281