PM2进程管理工具 [操作系统入门]
PM2是Node.js的进程管理工具" title="进程管理工具">进程管理工具,利用它可以非常容易地实现开机应用自动启动以及必要时自动启动的功能。
我们可以使用 pm2 来守护进程
以下示例采用node-red来演示。
[alick@devnet ~]# sudo npm install -g pm2
然后我们启动 node-red
[alick@devnet ~]# pm2 start node-red-------------__
/\\\\____/\____________/\____/\\\______
//////////\_/\\________/\\__////////\____/\_______/\_///\____///\_///______//\__
_/\\\/__/\/////_/\___________//___
_//////////____/\__////___/\________///_____
_/\_____________/\____///_____/\_____///________
_/\_____________/\_____________/\___//___________
_/\_____________/\_____________/\__/\\\\_
_///______________///______________///__///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /usr/bin/node-red in fork_mode (1 instance)
[PM2] Done.
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ? │ status │ cpu │ mem │ user │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ node-red │ default │ N/A │ fork │ 24229 │ 0s │ 0 │ online │ 0% │ 13.5mb │ root │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
重启所有 ProcessId
[alick@devnet ~]# pm2 restart allUse
--update-env to update environment variables[PM2] Applying action restartProcessId on app [all](ids: [
0 ])[PM2] [node
-red](0) ?┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │
namespace │ version │ mode │ pid │ uptime │ ? │ status │ cpu │ mem │ user │ watching │├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│
0 │ node-red │ default │ N/A │ fork │ 24257 │ 0s │ 1 │ online │ 0% │ 13.2mb │ root │ disabled │└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
查看 node-red 相关系统信息如下:
[alick@devnet ~]# pm2 info node-redDescribing process with id
0 - name node-red┌───────────────────┬────────────────────────────────────┐
│ status │ online │
│ name │ node
-red ││
namespace │ default ││ version │ N
/A ││ restarts │
1 ││ uptime │ 64s │
│ script path │
/usr/bin/node-red ││ script args │ N
/A ││ error log path │
/root/.pm2/logs/node-red-error.log ││
out log path │ /root/.pm2/logs/node-red-out.log ││ pid path │
/root/.pm2/pids/node-red-0.pid ││ interpreter │ node │
│ interpreter args │ N
/A ││ script id │
0 ││ exec cwd │
/root ││ exec mode │ fork_mode │
│ node.js version │
14.5.0 ││ node env │ N
/A ││ watch
& reload │ ? ││ unstable restarts │
0 ││ created at │
2020-07-09T10:38:40.795Z │└───────────────────┴────────────────────────────────────┘
Actions available
┌────────────────────────┐
│ km:heapdump │
│ km:cpu:profiling:start │
│ km:cpu:profiling:stop │
│ km:heap:sampling:start │
│ km:heap:sampling:stop │
└────────────────────────┘
Trigger via: pm2 trigger node
-red <action_name>Code metrics value
┌────────────────────────┬───────────┐
│ Heap Size │
26.41 MiB ││ Heap Usage │
93.64 % ││ Used Heap Size │
24.73 MiB ││ Active requests │
0 ││ Active handles │
4 ││ Event Loop Latency │
0.45 ms ││ Event Loop Latency p95 │
1.55 ms │└────────────────────────┴───────────┘
Divergent env variables
from local envAdd your own code metrics: http:
//bit.ly/code-metricsUse `pm2 logs node-red [--lines 1000]` to display logs
Use `pm2 env 0` to display environment variables
Use `pm2 monit` to monitor CPU and Memory usage node-red
查看 node-red 相关日志信息如下:
[alick@devnet ~]# pm2 logs node-red[TAILING] Tailing last
15 lines for [node-red] process (change the value with --lines option)/root/.pm2/logs/node-red-error.log last 15 lines:/root/.pm2/logs/node-red-out.log last 15 lines:0|node-red | ---------------------------------------------------------------------0|node-red | Your flow credentials file is encrypted using a system-generated key.
0|node-red |
0|node-red | If the system-generated key is lost for any reason, your credentials
0|node-red | file will not be recoverable, you will have to delete it and re-enter
0|node-red | your credentials.
0|node-red |
0|node-red | You should set your own key using the ‘credentialSecret‘ option in
0|node-red | your settings file. Node-RED will then re-encrypt your credentials
0|node-red | file using your chosen key the next time you deploy a change.
0|node-red | ---------------------------------------------------------------------
0|node-red |
0|node-red | 9 Jul 18:38:45 - [info] Starting flows
0|node-red | 9 Jul 18:38:45 - [info] Started flows
0|node-red | 9 Jul 18:38:45 - [info] Server now running at http://127.0.0.1:1880/
保存现在的 process 信息然后,设置为开机启动
[alick@devnet ~]# pm2 save[PM2] Saving current process list...
[PM2] Successfully saved
in /root/.pm2/dump.pm2[alick@devnet
~]# pm2 startup[PM2] Init System found: systemd
Platform systemd
Template
[Unit]
Description
=PM2 process managerDocumentation
=https://pm2.keymetrics.io/After=network.target
[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure
ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/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‘ ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] 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.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save
[PM2] Remove init script via:
$ pm2 unstartup systemd
访问浏览器界面,我们可以通过 1880 端口来正常访问 Node-RED 了。
PM2 进程管理工具
以上是 PM2进程管理工具 [操作系统入门] 的全部内容, 来源链接: utcz.com/z/519525.html