📔

CentOS7下使用pm2相关命令

执行命令pm2 start 进行启动
[root@centos7 server]# pm2 start app.js
[PM2] Starting /www/server/yapi/vendors/server/app.js in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬─────────┬──────┬───────┬────────┬─────────┬────────┬─────┬──────────┬──────┬──────────┐
│ App name │ id │ version │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem      │ user │ watching │
├──────────┼────┼─────────┼──────┼───────┼────────┼─────────┼────────┼─────┼──────────┼──────┼──────────┤
│ app      │ 01.7.1   │ fork │ 21501 │ online │ 0       │ 0s     │ 0%2.9 MB   │ root │ disabled │
└──────────┴────┴─────────┴──────┴───────┴────────┴─────────┴────────┴─────┴──────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app

保存当前列表

执行命令pm2 save保存现有列表数据,开机后会自动加载已保存的应用列表进行启动
[root@centos7 server]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
[root@centos7 server]#

设置开机启动

执行命令pm2 startup进行设置开机启动 输出以下信息就表明已经设置成功
[root@centos7 server]# pm2 startup
[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=/usr/local/src/node-v10.15.3-linux-x64/bin:/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/local/src/node-v10.15.3-linux-x64/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/src/node-v10.15.3-linux-x64/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/src/node-v10.15.3-linux-x64/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...
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd
[root@centos7 server]#

centos7 可以通过systemctl查看服务状态

[root@centos7 server]# systemctl status pm2-root
● pm2-root.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-root.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://pm2.keymetrics.io/
[root@centos7 server]#
其他相关命令:
# app指pm2创建的项目名
pm2 show app # 显示应用程序的所有信息
pm2 -h # 查看pm2帮助信息,显示命令用法
pm2 start app.js -n project # 将应用设置别名为project
pm2 list # 显示所有项目
pm2 stop app # 停止应用服务
pm2 restart app # 重启应用服务
pm2 delete app # 在进程中删除应用
pm2 status # 查看状态
pm2 monit # 查看占用的CPU和内存
来源链接:https://www.jianshu.com/p/316a4c562cfb/