官網:https://pm2.keymetrics.io/node
ADVANCED, PRODUCTION PROCESS MANAGER FOR NODE.JS高級,Node.js生產環境進程守護程序。git
npm install pm2@latest -g
$ pm2 start app.js -i 4 # 後臺運行pm2,啓動4個app.js # 也能夠把'max' 參數傳遞給 start # 正確的進程數目依賴於Cpu的核心數目 $ pm2 start app.js -i max # 根據有效CPU數目啓動最大進程數目 $ pm2 start app.js --name my-api # 命名進程 $ pm2 list # 顯示全部進程狀態 $ pm2 monit # 監視全部進程 $ pm2 logs # 顯示全部進程日誌 $ pm2 stop all # 中止全部進程 $ pm2 restart all # 重啓全部進程 $ pm2 reload all # 0 秒停機重載進程 (用於 NETWORKED 進程) $ pm2 stop 0 # 中止指定的進程 $ pm2 restart 0 # 重啓指定的進程 $ pm2 startup # 產生 init 腳本 保持進程活着 $ pm2 web # 運行健壯的 computer API endpoint (http://localhost:9615) $ pm2 delete 0 # 殺死指定的進程 $ pm2 delete all # 殺死所有進程 $ pm2 info app # 參看name爲app的信息
pm2的日誌模塊默認是每個服務進程都分配兩個默認的日誌文件這兩個日誌文件存放於
/root/.pm2/logs
中,若是pm2管理5個服務,那麼該文件夾下總共有10個日誌文件,而且隨着時間不斷增長,很容易就會產生不少個上g的日誌文件,致使了服務器的磁盤空間不足的問題github
pm2 install pm2-logrotate
max_size
(Defaults to 10M
): When a file size becomes higher than this value it will rotate it (its possible that the worker check the file after it actually pass the limit) . You can specify the unit at then end: 10G
, 10M
, 10K
web
retain
(Defaults to 30
file logs): 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.npm
compress
(Defaults to false
): Enable compression via gzip for all rotated logsapi
dateFormat
(Defaults to YYYY-MM-DD_HH-mm-ss
) : Format of the data used the name the file of logbash
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
)app
1
)單位爲秒(控制模塊檢查log日誌大小的循環時間,默認30s檢查一次)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 :this
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) │ │ │ │ └───── month (1 - 12) │ │ │ └────────── day of month (1 - 31) │ │ └─────────────── hour (0 - 23) │ └──────────────────── minute (0 - 59) └───────────────────────── second (0 - 59, OPTIONAL)
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:<param> <value>
例如:
pm2 set pm2-logrotate:max_size 1K
(1KB)pm2 set pm2-logrotate:compress true
(compress logs when rotated)pm2 set pm2-logrotate:rotateInterval '*/1 * * * *'
(force rotate every minute)$ pm2 set pm2-logrotate:max_size 10M $ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss # 文件名時間格式 $ pm2 set pm2-logrotate:workerInterval 3600 $ pm2 set pm2-logrotate:rotateInterval 0 0 * * * $ pm2 set pm2-logrotate:TZ Asia/Shanghai # 中國時區
參考: https://www.jianshu.com/p/54bc346d2406 做者:kelvv