pm2 高级配置

官方文档

pm2 相关命令

pm2 start app.js --name my-api # 命名进程
pm2 start app.js -i 4 #后台运行pm2,启动4个app.js
pm2 start test.php # 运行php程序
pm2 list # 查看进程
pm2 stop all # 停止所有
pm2 restart all        # 重启所有进程
pm2 monit              # 监视所有进程
pm2 logs # 查看日志
pm2 log [app_id] # 查看日志
pm2 restart [app_id]
pm2 stop [app_id]
pm2 plus # 开启web监控页面,需要注册 pm2 的官方账号

支持参数有
--name new-name指定进程名称,默认以文件名作为进程名称
-i 0启动多少个进程,0为CPU核数
--watch开启实时监控,当文件有变动时自动重启

pm2 启动其他服务程序的配置方式,支持yamljsjson

https://pm2.keymetrics.io/docs/usage/application-declaration/
json示例

{
  "apps": [{
      "name": "queue1",                            
      "script": "think",                     
      "cwd": "./",                               
      "interpreter": "php",                        
      "args": "queue:work --daemon --queue xxx"
  }]
}

{
  "apps": [{
      "name": "queue2",                            
      "script": "java",                     
      "cwd": "./",                               
      "interpreter": "",                        
      "args": "--jar xxx.jar"
  }]
}

pm2 启动 bat 等不使用环境的脚本

pm2 start XXX.bat --interpreter none

pm2开机启动配置

开启pm2开机自启服务

systemctl start pm2-用户名

pm2-用户名/etc/systemd/system目录下

加载 pm2 save 后的配置

pm2 resurrect

日志管理

pm2 install pm2-logrotate
  • pm2-logrotate配置

  • max_size (默认 10M): 最大为多少时进行分割,例如: 10G, 10M, 10K

  • retain (Defaults to all): This number is the number of rotated logs that are keep at any one time, it means that if you have retain = 7 you will have at most 7 rotated logs and your current one.

  • compress (默认 false): 是否压缩日志

  • dateFormat (默认 YYYY-MM-DD_HH-mm-ss) : 日志格式

  • rotateModule (Defaults to true) : Rotate the log of pm2's module like other apps

  • workerInterval (Defaults to 30 in secs) : You can control at which interval the worker is checking the log's size (minimum is 1)

  • rotateInterval (Defaults to 0 0 * * * everyday at midnight): This cron is used to a force rotate when executed. We are using node-schedule to schedule cron, so all valid cron for node-schedule is valid cron for this option. Cron style :

  • TZ (Defaults to system time): This is the standard tz database timezone used to offset the log file saved. For instance, a value of Etc/GMT-1, with an hourly log, will save a file at hour 14 GMT with hour 13 GMT-1 in the log name.

修改日志配置

pm2 set pm2-logrotate:max_size 2048K // 设置切割日志大小
pm2 set pm2-logrotate:rotateInterval '* * */1 * *'     //每小时备份
pm2 set pm2-logrotate:compress true    //压缩
pm2 set pm2-logrotate:retain 3  //备份最多3份,也就是备份最进3小时的日志

你可能感兴趣的:(pm2 高级配置)