pm2文档里有pm2 startup的命令,这个命令的目的是保存你的进程,然后在重启的服务器后,运行保存的进程,即使是意外的重启。
The purpose of a startup hook is to save your process list and bring it back at machine restarts, even unexpected ones.
每个操作系统都有一个特定的工具来处理启动hook,pm2提供了更简单的方式去生成和配置它。
步骤如下:
- 在你需要保存的进程环境里,选择正确的node版本,然后执行pm2 startup命令。
1 | pm2 startup |
输出如下: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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/root/.nvm/versions/node/v8.9.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
ExecStart=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root',
'systemctl start pm2-root',
'systemctl daemon-reload',
'systemctl status pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
>>> Executing systemctl enable pm2-root
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[DONE]
>>> Executing systemctl start pm2-root
[DONE]
>>> Executing systemctl daemon-reload
[DONE]
>>> Executing systemctl status pm2-root
● pm2-root.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-07-30 21:10:51 CST; 142ms ago
Docs: https://pm2.keymetrics.io/
Main PID: 18797 (PM2 v2.9.3: God)
CGroup: /system.slice/pm2-root.service
‣ 18797 PM2 v2.9.3: God Daemon (/root/.pm2)
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: ┌──────────┬────┬─────────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: ├──────────┼────┼─────────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: │ file │ 0 │ cluster │ 7218 │ online │ 1 │ 4M │ 0% │ 68.7 MB │ root │ disabled │
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: │ file │ 1 │ cluster │ 7217 │ online │ 1 │ 4M │ 0% │ 71.3 MB │ root │ disabled │
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: │ file │ 2 │ cluster │ 7229 │ online │ 1 │ 4M │ 0% │ 72.0 MB │ root │ disabled │
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: │ file │ 3 │ cluster │ 7225 │ online │ 1 │ 4M │ 0% │ 68.5 MB │ root │ disabled │
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: └──────────┴────┴─────────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz pm2[25150]: Use `pm2 show <id|name>` to get more details about an app
Jul 30 21:10:51 izwz90ssdog3u3uaesujemz systemd[1]: Started PM2 process manager.
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
同样,我们可以通过–service-name来指定启动的服务名。1
pm2 startup --service-name file
输出格式略有不同,如下: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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/root/.nvm/versions/node/v8.9.0/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
ExecStart=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/root/.nvm/versions/node/v8.9.0/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
Target path
/etc/systemd/system/file.service
Command list
[ 'systemctl enable file',
'systemctl start file',
'systemctl daemon-reload',
'systemctl status file' ]
[PM2] Writing init configuration in /etc/systemd/system/file.service
[PM2] Making script booting at startup...
>>> Executing systemctl enable file
Created symlink from /etc/systemd/system/multi-user.target.wants/file.service to /etc/systemd/system/file.service.
[DONE]
>>> Executing systemctl start file
[DONE]
>>> Executing systemctl daemon-reload
[DONE]
>>> Executing systemctl status file
● file.service - PM2 process manager
Loaded: loaded (/etc/systemd/system/file.service; enabled; vendor preset: disabled)
Active: active (running) since Mon 2018-07-30 21:12:22 CST; 71ms ago
Docs: https://pm2.keymetrics.io/
Main PID: 25264 (PM2 v2.9.3: God)
CGroup: /system.slice/file.service
├─25264 PM2 v2.9.3: God Daemon (/root/.pm2)
├─25276 node /root/app/file/api/app.js
├─25277 node /root/app/file/api/app.js
├─25288 node /root/app/file/api/app.js
├─25295 node /root/app/file/api/app.js
├─25383 /bin/sh -c cd '/root/app/file/';LC_ALL=en_US.UTF-8 git remote
└─25385 git remote
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: ┌──────────┬────┬─────────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: │ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ user │ watching │
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: ├──────────┼────┼─────────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: │ file │ 0 │ cluster │ 25276 │ online │ 0 │ 0s │ 29% │ 22.0 MB │ root │ disabled │
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: │ file │ 1 │ cluster │ 25277 │ online │ 0 │ 0s │ 18% │ 22.0 MB │ root │ disabled │
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: │ file │ 2 │ cluster │ 25288 │ online │ 0 │ 0s │ 23% │ 22.0 MB │ root │ disabled │
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: │ file │ 3 │ cluster │ 25295 │ online │ 0 │ 0s │ 22% │ 22.0 MB │ root │ disabled │
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: └──────────┴────┴─────────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz pm2[25258]: Use `pm2 show <id|name>` to get more details about an app
Jul 30 21:12:22 izwz90ssdog3u3uaesujemz systemd[1]: Started PM2 process manager.
[DONE]
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
- 保存进程列表–pm2 save
在步骤1中,startup hook会列出将要保存的进程列表,执行1
pm2 save
将会把这些进程保存下来,并在/root/.pm2目录下生成dump文件。
Note: 如果删除了所有进程,然后重启了系统(or use pm2 update), 系统会重启所有刚刚保存在dump中的进程。这是为了防止空dump文件的bug。
如果要创建一个空的dump文件,需要先执行
1 | pm2 cleardump |
- 禁用系统的startup
如果不想用这个功能了,可以执行1
pm2 unstartup
- 用户权限
如果要在一个其他账号下运行重启hook,执行如下命令,在user是liang的账号,/home/liang的根目录执行file启动hook。
1 | pm2 startup file -u liang --hp /home/liang |
- 更新hook
更新startup hook,只需要执行如下命令即可:
1 | pm2 unstartup |
END