2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!2018年的文章移至github上,点我去!

Fork me on GitHub

NodeJs之pm2

pm2


pm2是一个进程管理工具,可以用它来管理你的node进程,并查看node进程的状态,当然也支持性能监控,进程守护,负载均衡等功能。

开发过程中建议时不时的参看官方详细命令行使用:命令行

pm2文档

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

posted on 2016-12-22 13:44  qize  阅读(17163)  评论(1编辑  收藏  举报

导航