pm2

参考

如果要想在服务端部署node.js 程序,让其持久化运行,就不能单单使用 npm start 命令运行,当然了,这样运行是毫无问题的,但是当关闭 xshell 窗口或者是关闭进程的时候(其实关闭 xshell 窗口相当于默认关闭进程),就无法访问对应的 node.js 服务端程序了。

服务器上的 node 应用需要用 pm2 的 **日志管理、进程管理、负载均衡、性能监控 **等功能。不管是出于稳定性、性能还是可观测性等目的。但是有 docker 的场景下,pm2 就真的不需要的了。可有可无捏。

安装

1
npm install -g pm2

启动!

1
2
3
4
5
6
7
8
# 启动
pm2 start ./dist/main.js
# 删除
pm2 delete <进程名|进程id>
# 查看进程
pm2 ps
# 监控面板
pm2 monit

日志管理

1
2
3
4
# 日志查看
pm2 logs <?进程名|进程id> --lines 100
# 日志清理
pm2 flush <?进程名|进程id>

进程自动重启

1
2
3
4
5
6
7
8
# 超过 200M 内存自动重启:
pm2 start xxx --max-memory-restart 200M
# 从 2s 开始每 3s 重启一次:
pm2 start xxx --cron-restart "2/3 * * * * *"
# 当文件内容改变自动重启
pm2 start xxx --watch
# 不自动重启
pm2 start xxx --no-autorestart

负载均衡

-i 指定该进程用多几个 cpu 核心运行

1
2
pm2 start app.js -i max
pm2 start app.js -i 0

还可以动态调整进程数,通过 <font style="color:rgb(37, 41, 51);">pm2 scale</font>

1
2
pm2 scale main 3
pm2 scale main +3

ecosystem.config.js

执行 <font style="color:rgb(37, 41, 51);">pm2 ecosystem</font>会创建一个配置文件, 然后 <font style="color:rgb(37, 41, 51);">pm2 start ecosystem.config.js</font> 就可以批量跑一批应用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module.exports = {
apps: [
{
script: "index.js",
watch: ".",
},
{
script: "./service-worker/",
watch: ["./service-worker"],
},
],

deploy: {
production: {
user: "SSH_USERNAME",
host: "SSH_HOSTMACHINE",
ref: "origin/master",
repo: "GIT_REPOSITORY",
path: "DESTINATION_PATH",
"pre-deploy-local": "",
"post-deploy":
"npm install && pm2 reload ecosystem.config.js --env production",
"pre-setup": "",
},
},
};

forever (不推荐了,已经有更好的 pm2 了)

  1. 第一步安装 forever
1
npm install forever 或者 npm install -g forever

  1. 第二步运行对应的 js
1
forver start index.js

注意 (你可能会遇到如下错误):

错误信息:

<font style="color:rgb(77, 77, 77);">forever: command not found</font>

原因: 以 Windows 来说,通常这种错误是因为没有配置好环境变量,解决方案也很简单就是配置好环境变量或者是使用绝对路径 **<font style="color:rgb(77, 77, 77);">(全局安装就没有这个问题了)</font>**

解决方式 (Linux 演示,这里我使用绝对路径):

如何找到绝对路径呢?

通过该命令可以获取 node.js 的安装模块, **<font style="color:rgb(77, 77, 77);">npm list -g –depth 0</font>**

├── ali-oss@6.1.0

├── forever@0.15.3

└── npm@6.4.1

再通过关键字搜索 **<font style="color:rgb(77, 77, 77);">find / -name forever</font>**

1
2
3
4
5
6
7
8
/home/youcong/mock-github-api/node_modules/forever
/home/youcong/mock-github-api/node_modules/forever/lib/forever
/home/youcong/mock-github-api/node_modules/forever/bin/forever
/home/youcong/mock-github-api/node_modules/.bin/forever
/home/youcong/nodejs/lib/node_modules/forever
/home/youcong/nodejs/lib/node_modules/forever/lib/forever
/home/youcong/nodejs/lib/node_modules/forever/bin/forever
/home/youcong/nodejs/bin/forever

最后通过 **<font style="color:rgb(77, 77, 77);">/home/youcong/mock-github-api/node_modules/forever/bin/forever start index.js</font>** 即可实现 node.js 服务端程序在 Linux 上持久运行。

forever 常用命令

1
2
3
4
5
forever start app.js //启动程序
forever stop app.js //关闭程序
forever start -l forever.log -o out.log -e err.log app.js //启动程序并输出日志
forever restart app.js //重启程序
forever list //查看正在运行的进程

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!