pm2
pm2是一個進程管理工具,可以用它來管理你的node進程,並查看node進程的狀態,當然也支持性能監控,進程守護,負載均衡等功能。
開發過程中建議時不時的參看官方詳細命令行使用:命令行
pm2的安裝與使用
安裝
npm install -g pm2
需要全局安裝
啟動進程/應用
pm2 start bin/www
[PM2] Starting E:\zz\zz_wb123\manage\trunk\back-end\bin\www in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ www │ 0 │ fork │ 3468 │ online │ 0 │ 0s │ 6% │ 11.4 MB │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
進程/應用重命名
pm2 start app.js --name wb123
添加進程/應用watch
pm2 start bin/www --watch
watching變成了enabled
[PM2] Applying action restartProcessId on app [www](ids: 0)
[PM2] [www](0) ✓
[PM2] Process successfully started
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ www │ 0 │ fork │ 7852 │ online │ 0 │ 0s │ 6% │ 15.4 MB │ enabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
注意:我們也可以通過配置文件來配置監視。
{
"watch": ["server", "client"], //要監視的文件
"ignore_watch" : ["node_modules", "client/img"], //不監視的文件
"watch_options": { //監視配置
"followSymlinks": false
}
}
watch:可以是布爾值,數組,默認是:false。
停止監視:
pm2 stop --watch 0
結束進程/應用
pm2 stop www
結束所有進程/應用
pm2 stop all
刪除進程/應用
pm2 delete www
$ pm2 delete www
[PM2] Applying action deleteProcessId on app [www](ids: 0)
[PM2] [www](0) ✓
┌──────────┬────┬──────┬─────┬────────┬─────────┬────────┬─────┬─────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
└──────────┴────┴──────┴─────┴────────┴─────────┴────────┴─────┴─────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
刪除所有進程/應用
pm2 delete all
列出所有進程/應用
pm2 list
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────────┤
│ app │ 1 │ fork │ 6676 │ online │ 6 │ 0s │ 111% │ 35.3 MB │ enabled │
│ www │ 0 │ fork │ 2796 │ online │ 0 │ 27s │ 0% │ 40.3 MB │ enabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
查看某個進程/應用具體情況
pm2 describe www
Describing process with id 0 - name www
┌───────────────────┬──────────────────────────────────────────────────┐
│ status │ online │
│ name │ www │
│ restarts │ 0 │
│ uptime │ 111s │
│ script path │ E:\zz\zz_wb123\manage\trunk\back-end\bin\www │
│ script args │ N/A │
│ error log path │ C:\Users\Administrator\.pm2\logs\www-error-0.log │
│ out log path │ C:\Users\Administrator\.pm2\logs\www-out-0.log │
│ pid path │ C:\Users\Administrator\.pm2\pids\www-0.pid │
│ interpreter │ node │
│ interpreter args │ N/A │
│ script id │ 0 │
│ exec cwd │ E:\zz\zz_wb123\manage\trunk\back-end │
│ exec mode │ fork_mode │
│ node.js version │ 4.4.5 │
│ watch & reload │ ✔ │
│ unstable restarts │ 0 │
│ created at │ 2016-12-22T05:19:44.067Z │
└───────────────────┴──────────────────────────────────────────────────┘
Code metrics value
┌────────────┬────────┐
│ Loop delay │ 0.01ms │
└────────────┴────────┘
Add your own code metrics: http://bit.ly/code-metrics
Use `pm2 logs www [--lines 1000]` to display logs
Use `pm2 monit` to monitor CPU and Memory usage www
查看進程/應用的資源消耗情況
pm2 monit
⌬ PM2 monitoring (To go further check out https://app.keymetrics.io)
● www [ ] 0 %
[0] [fork_mode] [||||||| ] 41.945 MB
● app [ ] errored
[1] [fork_mode] [ ] 0 B
重新啟動進程/應用
//pm2 reset [app-name]
pm2 restart www
$ pm2 restart www
Restarts are now immutable, to update environment or conf use --update-env
[PM2] Applying action restartProcessId on app [www](ids: 0)
[PM2] [www](0) ✓
┌──────────┬────┬──────┬──────┬─────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼──────┼─────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ app │ 1 │ fork │ 0 │ errored │ 15 │ 0 │ 0% │ 0 B │ enabled │
│ www │ 0 │ fork │ 3316 │ online │ 1 │ 0s │ 4% │ 11.2 MB │ enabled │
└──────────┴────┴──────┴──────┴─────────┴─────────┴────────┴─────┴───────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
如要重新啟動所有進程/應用,使用
pm2 restart all
配置文件
我們也可通過在項目根目錄下添加pm2的一個json的配置文件來控制我們的應用。
{
"name": "manage", //項目名稱
"script": "./bin/www", //要執行的腳本
"cwd":"./", //項目所在目錄
"watch":[ //添加受監控的文件,以便自動更新
"models",
"routes"
]
}
啟動后:
# pm2 list
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ cpu │ mem │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ manage │ 0 │ fork │ 9791 │ online │ 2 │ 46m │ 0% │ 95.3 MB │ enabled │
pm2模式
修改模式
默認啟動的是fork模式,修改成集群模式
$ pm2 start app.js -i max
配置文件配置
我們也可以通過配置文件配置模式
{
"apps" : [{
"script" : "app.js",
"instances" : "1",
"exec_mode" : "cluster"
}]
}
pm2日志
查看所有日志
pm2 logs
查看某個進程/應用的日志
//pm2 logs [app-name]
pm2 logs www
$ pm2 logs www
[TAILING] Tailing last 10 lines for [www] process (change the value with --lines option)
C:\Users\Administrator\.pm2\logs\www-error-0.log last 10 lines:
C:\Users\Administrator\.pm2\logs\www-out-0.log last 10 lines:
0|www | 使用備用db配置
0|www | Listening on port 3000
0|www | 使用備用db配置
0|www | Listening on port 3000
0|www | 使用備用db配置
0|www | Listening on port 3000
0|www | 使用備用db配置
0|www | Listening on port 3000
0|www | 使用備用db配置
0|www | Listening on port 3000
[STREAMING] Now streaming realtime logs for [www] process
查看指定行數開始的日志
# pm2 logs manage --lines 10
json格式查看日志
# pm2 logs manage --json
日志的輸出配置
{
"script" : "echo.js",
"error_file" : "err.log",
"out_file" : "out.log",
"merge_logs" : true,
"log_date_format" : "YYYY-MM-DD HH:mm Z"
}
合並日志
{
"log_file": "combined.outerr.log",
"out_file": "out.log",
"error_file": "err.log"
}
將錯誤文件與輸出文件合並到combined.outerr.log中。
查看pm2的運行日志
# pm2 logs
[TAILING] Tailing last 10 lines for [all] processes (change the value with --lines option)
/root/.pm2/pm2.log last 10 lines:
PM2 | 2016-12-28 16:04:01: pid=26706 msg=process killed
PM2 | 2016-12-28 16:04:01: pid=30925 msg=process killed
PM2 | 2016-12-28 16:04:15: [Watch] Start watching 0
PM2 | 2016-12-28 16:04:15: Starting execution sequence in -fork mode- for app name:manage id:0
PM2 | 2016-12-28 16:04:15: App name:manage id:0 online
PM2 | 2016-12-28 16:07:06: Starting execution sequence in -fork mode- for app name:test_wb123 id:1
PM2 | 2016-12-28 16:07:06: App name:test_wb123 id:1 online
PM2 | 2016-12-28 18:14:26: Reloading logs...
PM2 | 2016-12-28 18:14:26: Reloading logs for process id 0
PM2 | 2016-12-28 18:14:26: Reloading logs for process id 1
/root/.pm2/logs/manage-out-0.log last 10 lines:
/root/.pm2/logs/manage-error-0.log last 10 lines:
/root/.pm2/logs/test-wb123-out-1.log last 10 lines:
/root/.pm2/logs/test-wb123-error-1.log last 10 lines:
這個我們就能找到pm2中的日志,以及我們為輸出的日志,這很重要,對於排除莫名其妙的問題!
進入我們的.pm2的文件中。
[root@Zhaoqize 18:35 ~/.pm2]
# ls
dump.pm2 ecosystem.config.js logs module_conf.json pids pm2.log pm2.pid pub.sock rpc.sock touch
建議:去百度問題前,先看下日志情況,確定問題范圍,然后stackoverflow