进程守护神器-PM2

PM2是一个守护进程管理器,它可以帮助你管理和保持应用程序24/7在线,与PM2类似是进程守护工具还有Supervisor、Forever等,在此,我们先学习一下PM2。

安装PM2

有两种方式,第一种就是基于NPM安装:

1
npm install pm2 -g

另外一种就是直接下载:

1
wget -qO- https://getpm2.com/install.sh | bash

我才用的后者:

1
2
3
4
5
6
7
8
9
10
[root@VM_175_142_centos ~]# wget -qO- https://getpm2.com/install.sh | bash
> Welcome to the PM2 auto installer

┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2] Setting changed
Module: pm2
$ pm2 set pm2:autosave true
PM2 Successfully installed

提示我们安装成功,我们来检验一下:

1
2
[root@VM_175_142_centos ~]# pm2 -v
4.2.1

这证明我们安装成功了。

PM2常用命令

我们先来归纳一下常用的命令,后面部分会进入到实践操作环节。

  • pm2 start [Node.js, Python, Ruby, binaries in $PATH] #启动应用
  • pm2 [list|ls|status] #列出所有应用程序管理的状态
  • pm2 stop <app_name|namespace|id|’all’|json_conf> #停止应用
  • pm2 restart <app_name|namespace|id|’all’|json_conf> #重启应用
  • pm2 delete <app_name|namespace|id|’all’|json_conf> #删除应用
  • pm2 describe <id|app_name> #查看应用详细信息
  • pm2 monit #应用监控信息
  • pm2 logs #查看应用日志
  • pm2 install #安装应用模块
  • pm2 ecosystem #生成配置文件

升级PM2:

  • npm install pm2@latest -g #下载最新版本的PM2
  • pm2 update #在内存中更新PM2

露一手

我的centos有一个go程序,我们来solo一番。
启动我们的应用

1
2
3
4
5
6
7
8
9
[root@VM_175_142_centos shorturl]# pm2 start main
[PM2] Starting /root/shorturl/main in fork_mode (1 instance)
[PM2] Done.
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 24044 │ 0s │ 0 │ online │ 0% │ 1.5mb │ root │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

查看我们的应用

1
2
3
4
5
6
7
[root@VM_175_142_centos shorturl]# pm2 list
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 24044 │ 68s │ 0 │ online │ 0% │ 20.1mb │ root │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

重启应用

1
2
3
4
5
6
7
8
9
10
[root@VM_175_142_centos shorturl]# pm2 restart main
Use --update-env to update environment variables
[PM2] Applying action restartProcessId on app [main](ids: [ 0 ])
[PM2] [main](0) ✓
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 2078 │ 0s │ 1 │ online │ 0% │ 1.5mb │ root │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

停止应用

1
2
3
4
5
6
7
8
9
[root@VM_175_142_centos shorturl]# pm2 stop main
[PM2] Applying action stopProcessId on app [main](ids: [ 0 ])
[PM2] [main](0) ✓
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 0 │ 0 │ 1 │ stopped │ 0% │ 0b │ root │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

可以看到我们的应用状态为stopped,我们可以查看我们的日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@VM_175_142_centos shorturl]# pm2 logs main
[TAILING] Tailing last 15 lines for [main] process (change the value with --lines option)
/root/.pm2/logs/main-out.log last 15 lines:
/root/.pm2/logs/main-error.log last 15 lines:
0|main | time="2019-12-12T13:31:55+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:33:01+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:33:01+08:00" level=info msg="Server shutdown"
0|main | time="2019-12-12T13:34:12+08:00" level=info msg="Creating DB with [ bolt ]"
0|main | time="2019-12-12T13:34:12+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:50:30+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:50:30+08:00" level=info msg="Server shutdown"
0|main | time="2019-12-12T13:52:26+08:00" level=info msg="Creating DB with [ bolt ]"
0|main | time="2019-12-12T13:52:26+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Server shutdown"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Creating DB with [ bolt ]"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:55:17+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:55:17+08:00" level=info msg="Server shutdown"

查看前10行的日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 [root@VM_175_142_centos shorturl]# pm2 logs main --lines 10
[TAILING] Tailing last 10 lines for [main] process (change the value with --lines option)
/root/.pm2/logs/main-out.log last 10 lines:
/root/.pm2/logs/main-error.log last 10 lines:
0|main | time="2019-12-12T13:50:30+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:50:30+08:00" level=info msg="Server shutdown"
0|main | time="2019-12-12T13:52:26+08:00" level=info msg="Creating DB with [ bolt ]"
0|main | time="2019-12-12T13:52:26+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Server shutdown"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Creating DB with [ bolt ]"
0|main | time="2019-12-12T13:54:47+08:00" level=info msg="Server running at [ http://0.0.0.0:80 ], with domain [ https://52fx.biz ]"
0|main | time="2019-12-12T13:55:17+08:00" level=info msg="Shuting down the server"
0|main | time="2019-12-12T13:55:17+08:00" level=info msg="Server shutdown"

查看应用状态
1
2
3
4
5
6
7
 [root@VM_175_142_centos shorturl]# pm2 status main
┌─────┬─────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 0 │ 0 │ 1 │ stopped │ 0% │ 0b │ root │ disabled │
└─────┴─────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

查看监控
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
 [root@VM_175_142_centos shorturl]# pm2 monit
┌─ Process List ─────────────────────────────────────────┐┌── main Logs ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│[ 0] main Mem: 19 MB CPU: 0 % online ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
│ ││ │
└────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
┌─ Custom Metrics ───────────────────────────────────────┐┌─ Metadata ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ ││ App Name main │
│ ││ Namespace default │
│ ││ Version N/A │
│ ││ Restarts 1 │
│ ││ Uptime 31s │
│ ││ Script path /root/shorturl/main │
└────────────────────────────────────────────────────────┘└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
left/right: switch boards | up/down/mouse: scroll | Ctrl-C: exit

PM2配置方式启动
1
2
 [root@VM_175_142_centos shorturl]# pm2 ecosystem 
File /root/shorturl/ecosystem.config.js generated

会生成一个名为ecosystem.config.js的模版配置文件,我们稍作修改:
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
 module.exports = {
apps : [{
name: 'short',
script: 'main',

// Options reference: https://pm2.keymetrics.io/docs/usage/application-declaration/
args: 'config ./config.yml',
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '1G',
env: {
NODE_ENV: 'development'
},
env_production: {
NODE_ENV: 'production'
}
}],

deploy : {
production : {
user : 'node',
host : '212.83.163.1',
ref : 'origin/master',
repo : 'git@github.com:repo.git',
path : '/var/www/production',
'post-deploy' : 'npm install && pm2 reload ecosystem.config.js --env production'
}
}
};

运行:

1
2
3
4
5
6
7
8
9
10
[root@VM_175_142_centos shorturl]# pm2 start ecosystem.config.js
[PM2][WARN] Applications short not running, starting...
[PM2] App [short] launched (1 instances)
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ main │ default │ N/A │ fork │ 0 │ 0 │ 1 │ stopped │ 0% │ 0b │ root │ disabled │
│ 1 │ short │ default │ N/A │ fork │ 9525 │ 0s │ 0 │ online │ 0% │ 1.5mb │ root │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
[PM2][WARN] Current process list running is not in sync with saved list. Type 'pm2 save' to synchronize or enable autosync via 'pm2 set pm2:autodump true'

具体的参数说明,可以去官网查阅.

未完待续,后面又更多骚操作,会继续更新该文档

#
You forgot to set the qrcode for Alipay. Please set it in _config.yml.
You forgot to set the qrcode for Wechat. Please set it in _config.yml.
You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×