pm2 相關(guān)命令
pm2 start app.js --name my-api # 命名進(jìn)程
pm2 start app.js -i 4 #后臺(tái)運(yùn)行pm2世分,啟動(dòng)4個(gè)app.js
pm2 start test.php # 運(yùn)行php程序
pm2 list # 查看進(jìn)程
pm2 stop all # 停止所有
pm2 restart all # 重啟所有進(jìn)程
pm2 monit # 監(jiān)視所有進(jìn)程
pm2 logs # 查看日志
pm2 log [app_id] # 查看日志
pm2 restart [app_id]
pm2 stop [app_id]
pm2 plus # 開(kāi)啟web監(jiān)控頁(yè)面泽西,需要注冊(cè) pm2 的官方賬號(hào)
支持參數(shù)有
--name new-name
指定進(jìn)程名稱玫锋,默認(rèn)以文件名作為進(jìn)程名稱
-i 0
啟動(dòng)多少個(gè)進(jìn)程,0為CPU
核數(shù)
--watch
開(kāi)啟實(shí)時(shí)監(jiān)控未状,當(dāng)文件有變動(dòng)時(shí)自動(dòng)重啟
pm2 啟動(dòng)其他服務(wù)程序的配置方式,支持yaml
晶框,js
和json
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 啟動(dòng) bat 等不使用環(huán)境的腳本
pm2 start XXX.bat --interpreter none
pm2開(kāi)機(jī)啟動(dòng)配置
開(kāi)啟pm2開(kāi)機(jī)自啟服務(wù)
systemctl start pm2-用戶名
pm2-用戶名
在/etc/systemd/system
目錄下
加載 pm2 save 后的配置
pm2 resurrect
日志管理
pm2 install pm2-logrotate
pm2-logrotate配置
max_size
(默認(rèn) 10M): 最大為多少時(shí)進(jìn)行分割闰蚕,例如: 10G, 10M, 10Kretain
(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 (默認(rèn) false): 是否壓縮日志
dateFormat (默認(rèn) 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 // 設(shè)置切割日志大小
pm2 set pm2-logrotate:rotateInterval '* * */1 * *' //每小時(shí)備份
pm2 set pm2-logrotate:compress true //壓縮
pm2 set pm2-logrotate:retain 3 //備份最多3份栈拖,也就是備份最進(jìn)3小時(shí)的日志