pm2 简明使用教程

安装

npm install pm2@latest -g

书写配置文件

使用配置文件来启动,便于统一管理

~/.config/pm2/ecosystem.config.yaml

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
apps:
- script : /home/luohao/applications/code-server-3.9.1-linux-amd64/bin/code-server
interpreter: none
max_memory_restart: 500M
name : 'code-server'
instances : 1
exec_mode : fork
env:
http_proxy : http://127.0.0.1:4411
https_proxy: http://127.0.0.1:4411
- script : /home/luohao/applications/cow-linux64-0.9.8
interpreter: none
name : 'cow'
instances : 1
exec_mode : fork
env:
version: 0.9.8
- script : /home/luohao/applications/kcptun/client_linux_amd64 -c /home/luohao/applications/kcptun/kcptun.json
interpreter: none
name : 'kcptun-client'
instances : 1
exec_mode : fork
- script : /usr/bin/ss-local -c /home/luohao/applications/shadowsocks/ss.json
interpreter: none
name : 'shadowsocks-client'
instances : 1
exec_mode : fork
- script : /home/luohao/.local/bin/projector run Goland
interpreter: none
name : 'Goland-server'
instances : 1
exec_mode : fork
- script : /home/luohao/.local/bin/projector run Webstorm
interpreter: none
name : 'Webstorm-server'
instances : 1
exec_mode : fork
- script : /home/luohao/.local/bin/projector run Clion
interpreter: none
name : 'Clion-server'
instances : 1
exec_mode : fork

interpreter 设置为 none,用默认行为启动二进制文件。以上是服务器设置客户端代理的脚本,供参考

启动

1
pm2 start ~/.config/pm2/ecosystem.config.yaml

常用的操作还有下面的系列操作,可以在后面加上 jobId(通过 pm2 list查看),或者加上 echosystem 文件

script
1
2
3
4
5
6
7
8
9
pm2 delete JOBID_OR_FILE
pm2 stop JOBID_OR_FILE
pm2 reload JOBID_OR_FILE
pm2 restart JOBID_OR_FILE
pm2 descibe JOBID_OR_FILE

# 监控类
pm2 monit
pm2 list

监控

https://app.pm2.io/

设置为开机自启动

1
2
3
4
5
6
7
pm2 startup

# 输入完 之后有如下提示
# sudo env PATH=$PATH:/home/luohao/.nvm/versions/node/v15.11.0/bin /home/luohao/.nvm/versions/node/v15.11.0/lib/node_modules/pm2/bin/pm2 startup systemd -u luohao --hp /home/luohao

# 更改完配置后更新最新配置
pm2 save