当前位置: 首页 > 工具软件 > pm2Manage > 使用案例 >

pm2 使用

勾炜
2023-12-01

 

pm2


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

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

pm2文档

pm2的安装与使用


安装

<span style="color:#000000"><code>npm install -g pm2</code></span>

需要全局安装

启动进程/应用

<span style="color:#000000"><code>pm2 start bin/www</code></span>
[PM2] Starting E:\zz\zz_wb123\manage\trunk\back-end\bin\www in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App nameidmode │ 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

进程/应用重命名

<span style="color:#000000"><code>pm2 start app.js --name wb123</code></span>

添加进程/应用watch

<span style="color:#000000"><code> pm2 start bin/www --watch</code></span>

watching变成了enabled

<span style="color:#000000"><code>[PM2] Applying action restartProcessId on app [www](ids: <span style="color:#880000"><span style="color:#880000">0</span></span>)
[PM2] [www](<span style="color:#880000"><span style="color:#880000">0</span></span>) ✓
[PM2] Process successfully started
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem       │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ www      │ <span style="color:#880000"><span style="color:#880000">0</span></span>  │ fork │ <span style="color:#880000"><span style="color:#880000">7852</span></span> │ online │ <span style="color:#880000"><span style="color:#880000">0</span></span>       │ <span style="color:#880000"><span style="color:#880000">0</span></span>s     │ <span style="color:#880000"><span style="color:#880000">6</span></span>%  │ <span style="color:#880000"><span style="color:#880000">15.4</span></span> MB   │ enabled  │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────┴───────────┴──────────┘
 Use <span style="color:#880000"><span style="color:#880000">`pm2 show <id|name>`</span></span> to get more details about an app</code></span>

注意:我们也可以通过配置文件来配置监视。

<span style="color:#000000"><code>{
  <span style="color:#ff0000"><span style="color:#ff0000">"watch"</span></span>: [<span style="color:#880000"><span style="color:#880000">"server"</span></span>, <span style="color:#880000"><span style="color:#880000">"client"</span></span>], //要监视的文件
  <span style="color:#ff0000"><span style="color:#ff0000">"ignore_watch"</span></span> : [<span style="color:#880000"><span style="color:#880000">"node_modules"</span></span>, <span style="color:#880000"><span style="color:#880000">"client/img"</span></span>], //不监视的文件
  <span style="color:#ff0000"><span style="color:#ff0000">"watch_options"</span></span>: {  //监视配置
    <span style="color:#ff0000"><span style="color:#ff0000">"followSymlinks"</span></span>: <span style="color:#78a960"><span style="color:#78a960">false</span></span>
  }
}</code></span>

watch:可以是布尔值,数组,默认是:false。

停止监视:

<span style="color:#000000"><code>pm2 stop --watch 0</code></span>

结束进程/应用

<span style="color:#000000"><code>pm2 stop www</code></span>

结束所有进程/应用

<span style="color:#000000"><code>pm2 stop all</code></span>

删除进程/应用

<span style="color:#000000"><code>pm2 <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>delete</strong></span></strong></span> www</code></span>
 $  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

删除所有进程/应用

<span style="color:#000000"><code>pm2 <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>delete</strong></span></strong></span> all</code></span>

列出所有进程/应用

<span style="color:#000000"><code>pm2 list</code></span>
<span style="color:#000000"><code>┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬──────┬───────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu  │ mem       │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼──────┼───────────┼──────────┤
│ app      │ <span style="color:#880000"><span style="color:#880000">1</span></span>  │ fork │ <span style="color:#880000"><span style="color:#880000">6676</span></span> │ online │ <span style="color:#880000"><span style="color:#880000">6</span></span>       │ <span style="color:#880000"><span style="color:#880000">0</span></span>s     │ <span style="color:#880000"><span style="color:#880000">111</span></span>% │ <span style="color:#880000"><span style="color:#880000">35.3</span></span> MB   │ enabled  │
│ www      │ <span style="color:#880000"><span style="color:#880000">0</span></span>  │ fork │ <span style="color:#880000"><span style="color:#880000">2796</span></span> │ online │ <span style="color:#880000"><span style="color:#880000">0</span></span>       │ <span style="color:#880000"><span style="color:#880000">27</span></span>s    │ <span style="color:#880000"><span style="color:#880000">0</span></span>%   │ <span style="color:#880000"><span style="color:#880000">40.3</span></span> MB   │ enabled  │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴──────┴───────────┴──────────┘
 Use <span style="color:#880000"><span style="color:#880000">`pm2 show <id|name>`</span></span> to get more details about an app</code></span>

查看某个进程/应用具体情况

<span style="color:#000000"><code>pm2 describe www</code></span>
<span style="color:#000000"><code> Describing process <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>with</strong></span></strong></span> id <span style="color:#880000"><span style="color:#880000">0</span></span> - name www
┌───────────────────┬──────────────────────────────────────────────────┐
│ status            │ online                                           │
│ name              │ www                                              │
│ restarts          │ <span style="color:#880000"><span style="color:#880000">0</span></span>                                                │
│ uptime            │ <span style="color:#880000"><span style="color:#880000">111</span></span>s                                             │
│ 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<span style="color:#880000"><span style="color:#880000">-0.</span></span>log │
│ out log path      │ C:\Users\Administrator\.pm2\logs\www-out<span style="color:#880000"><span style="color:#880000">-0.</span></span>log   │
│ pid path          │ C:\Users\Administrator\.pm2\pids\www<span style="color:#880000"><span style="color:#880000">-0.</span></span>pid       │
│ interpreter       │ node                                             │
│ interpreter args  │ N/A                                              │
│ script id         │ <span style="color:#880000"><span style="color:#880000">0</span></span>                                                │
│ exec cwd          │ E:\zz\zz_wb123\manage\trunk\back-end             │
│ exec mode         │ fork_mode                                        │
│ node.js version   │ <span style="color:#880000"><span style="color:#880000">4.4</span></span><span style="color:#880000"><span style="color:#880000">.5</span></span>                                            │
│ watch & reload    │ ✔                                                │
│ unstable restarts │ <span style="color:#880000"><span style="color:#880000">0</span></span>                                                │
│ created at        │ <span style="color:#880000"><span style="color:#880000">2016</span></span><span style="color:#880000"><span style="color:#880000">-12</span></span><span style="color:#880000"><span style="color:#880000">-22</span></span>T05:<span style="color:#880000"><span style="color:#880000">19</span></span>:<span style="color:#880000"><span style="color:#880000">44.067</span></span>Z                         │
└───────────────────┴──────────────────────────────────────────────────┘
 Code metrics value
┌────────────┬────────┐
│ Loop delay │ <span style="color:#880000"><span style="color:#880000">0.01</span></span>ms │
└────────────┴────────┘
 Add your own code metrics: http:<span style="color:#888888"><span style="color:#888888">//bit.ly/code-metrics</span></span>
 Use <span style="color:#880000"><span style="color:#880000">`pm2 logs www [--lines 1000]`</span></span> to display logs
 Use <span style="color:#880000"><span style="color:#880000">`pm2 monit`</span></span> to monitor CPU and Memory usage www</code></span>

查看进程/应用的资源消耗情况

<span style="color:#000000"><code>pm2 monit</code></span>
<span style="color:#000000"><code>⌬ PM2 monitoring (To go further check out https:<span style="color:#888888"><span style="color:#888888">//app.keymetrics.io)</span></span>

 ● www                                [                              ] <span style="color:#880000"><span style="color:#880000">0</span></span> %
[<span style="color:#880000"><span style="color:#880000">0</span></span>] [fork_mode]                        [|||||||                       ] <span style="color:#880000"><span style="color:#880000">41.945</span></span> MB

 ● app                                [                              ] errored
[<span style="color:#880000"><span style="color:#880000">1</span></span>] [fork_mode]                        [                              ] <span style="color:#880000"><span style="color:#880000">0</span></span> B</code></span>

重新启动进程/应用

<span style="color:#000000"><code><span style="color:#888888"><span style="color:#888888">//pm2 reset [app-name]</span></span>
 pm2 restart www</code></span>
<span style="color:#000000"><code> $  pm2 restart www
Restarts are now immutable, to update environment or conf use --update-env
[PM2] Applying action restartProcessId on app [www](ids: <span style="color:#880000"><span style="color:#880000">0</span></span>)
[PM2] [www](<span style="color:#880000"><span style="color:#880000">0</span></span>) ✓
┌──────────┬────┬──────┬──────┬─────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid  │ status  │ restart │ uptime │ cpu │ mem       │ watching │
├──────────┼────┼──────┼──────┼─────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ app      │ <span style="color:#880000"><span style="color:#880000">1</span></span>  │ fork │ <span style="color:#880000"><span style="color:#880000">0</span></span>    │ errored │ <span style="color:#880000"><span style="color:#880000">15</span></span>      │ <span style="color:#880000"><span style="color:#880000">0</span></span>      │ <span style="color:#880000"><span style="color:#880000">0</span></span>%  │ <span style="color:#880000"><span style="color:#880000">0</span></span> B       │ enabled  │
│ www      │ <span style="color:#880000"><span style="color:#880000">0</span></span>  │ fork │ <span style="color:#880000"><span style="color:#880000">3316</span></span> │ online  │ <span style="color:#880000"><span style="color:#880000">1</span></span>       │ <span style="color:#880000"><span style="color:#880000">0</span></span>s     │ <span style="color:#880000"><span style="color:#880000">4</span></span>%  │ <span style="color:#880000"><span style="color:#880000">11.2</span></span> MB   │ enabled  │
└──────────┴────┴──────┴──────┴─────────┴─────────┴────────┴─────┴───────────┴──────────┘
 Use <span style="color:#880000"><span style="color:#880000">`pm2 show <id|name>`</span></span> to get more details about an app</code></span>

如要重新启动所有进程/应用,使用

<span style="color:#000000"><code>pm2 restart all</code></span>

配置文件


我们也可通过在项目根目录下添加pm2的一个json的配置文件来控制我们的应用。

<span style="color:#000000"><code>{
    <span style="color:#880000"><span style="color:#880000">"name"</span></span>: <span style="color:#880000"><span style="color:#880000">"manage"</span></span>,  <span style="color:#888888"><span style="color:#888888">//项目名称</span></span>
    <span style="color:#880000"><span style="color:#880000">"script"</span></span>: <span style="color:#880000"><span style="color:#880000">"./bin/www"</span></span>, <span style="color:#888888"><span style="color:#888888">//要执行的脚本</span></span>
    <span style="color:#880000"><span style="color:#880000">"cwd"</span></span>:<span style="color:#880000"><span style="color:#880000">"./"</span></span>, <span style="color:#888888"><span style="color:#888888">//项目所在目录</span></span>
    <span style="color:#880000"><span style="color:#880000">"watch"</span></span>:[  <span style="color:#888888"><span style="color:#888888">//添加受监控的文件,以便自动更新</span></span>
        <span style="color:#880000"><span style="color:#880000">"models"</span></span>,
        <span style="color:#880000"><span style="color:#880000">"routes"</span></span>
    ]
}</code></span>

启动后:

<span style="color:#000000"><code><span style="color:#888888"><span style="color:#888888"># pm2 list</span></span>
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────┬───────────┬──────────┐
│ App name │ id │ mode │ pid  │ status │ restart │ uptime │ cpu │ mem       │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────┼───────────┼──────────┤
│ manage   │ 0  │ fork │ 9791 │ online │ 2       │ 46m    │ 0%  │ 95.3 MB   │ enabled  │
</code></span>

pm2模式


修改模式

默认启动的是fork模式,修改成集群模式

<span style="color:#000000"><code>$ pm2 start  app.js -i max</code></span>

配置文件配置

我们也可以通过配置文件配置模式

<span style="color:#000000"><code>{
  <span style="color:#880000"><span style="color:#880000">"apps"</span></span> : [{
    <span style="color:#880000"><span style="color:#880000">"script"</span></span>    : <span style="color:#880000"><span style="color:#880000">"app.js"</span></span>,
    <span style="color:#880000"><span style="color:#880000">"instances"</span></span> : <span style="color:#880000"><span style="color:#880000">"1"</span></span>,
    <span style="color:#880000"><span style="color:#880000">"exec_mode"</span></span> : <span style="color:#880000"><span style="color:#880000">"cluster"</span></span>
  }]
}</code></span>

pm2日志


查看所有日志

<span style="color:#000000"><code>pm2 logs</code></span>

查看某个进程/应用的日志

<span style="color:#000000"><code><span style="color:#888888"><span style="color:#888888">//pm2 logs [app-name]</span></span>
pm2 logs www</code></span>
<span style="color:#000000"><code> $  pm2 logs www
[TAILING] Tailing last <span style="color:#880000"><span style="color:#880000">10</span></span> lines <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> [www] process (change the value <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>with</strong></span></strong></span> --lines option)
C:\Users\Administrator\.pm2\logs\www-error<span style="color:#880000"><span style="color:#880000">-0.</span></span>log last <span style="color:#880000"><span style="color:#880000">10</span></span> lines:
C:\Users\Administrator\.pm2\logs\www-out<span style="color:#880000"><span style="color:#880000">-0.</span></span>log last <span style="color:#880000"><span style="color:#880000">10</span></span> lines:
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | 使用备用db配置
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | Listening on port <span style="color:#880000"><span style="color:#880000">3000</span></span>
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | 使用备用db配置
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | Listening on port <span style="color:#880000"><span style="color:#880000">3000</span></span>
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | 使用备用db配置
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | Listening on port <span style="color:#880000"><span style="color:#880000">3000</span></span>
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | 使用备用db配置
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | Listening on port <span style="color:#880000"><span style="color:#880000">3000</span></span>
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | 使用备用db配置
<span style="color:#880000"><span style="color:#880000">0</span></span>|www      | Listening on port <span style="color:#880000"><span style="color:#880000">3000</span></span>

[STREAMING] Now streaming realtime logs <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> [www] process</code></span>

查看指定行数开始的日志

<span style="color:#000000"><code># pm2 logs manage --lines <span style="color:#880000"><span style="color:#880000">10</span></span></code></span>

json格式查看日志

<span style="color:#000000"><code># pm2 logs manage --json</code></span>

日志的输出配置

<span style="color:#000000"><code>{
  <span style="color:#880000"><span style="color:#880000">"script"</span></span>          : <span style="color:#880000"><span style="color:#880000">"echo.js"</span></span>,
  <span style="color:#880000"><span style="color:#880000">"error_file"</span></span>      : <span style="color:#880000"><span style="color:#880000">"err.log"</span></span>,
  <span style="color:#880000"><span style="color:#880000">"out_file"</span></span>        : <span style="color:#880000"><span style="color:#880000">"out.log"</span></span>,
  <span style="color:#880000"><span style="color:#880000">"merge_logs"</span></span>      : <span style="color:#78a960"><span style="color:#78a960">true</span></span>,
  <span style="color:#880000"><span style="color:#880000">"log_date_format"</span></span> : <span style="color:#880000"><span style="color:#880000">"YYYY-MM-DD HH:mm Z"</span></span>
}</code></span>

合并日志

<span style="color:#000000"><code>{
  <span style="color:#880000"><span style="color:#880000">"log_file"</span></span>: <span style="color:#880000"><span style="color:#880000">"combined.outerr.log"</span></span>,
  <span style="color:#880000"><span style="color:#880000">"out_file"</span></span>: <span style="color:#880000"><span style="color:#880000">"out.log"</span></span>,
  <span style="color:#880000"><span style="color:#880000">"error_file"</span></span>: <span style="color:#880000"><span style="color:#880000">"err.log"</span></span>
}</code></span>

将错误文件与输出文件合并到combined.outerr.log中。

查看pm2的运行日志

<span style="color:#000000"><code><span style="color:#888888"><span style="color:#888888"># pm2 logs</span></span>
[TAILING] Tailing last 10 lines <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> [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 <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>in</strong></span></strong></span> -fork mode- <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> 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 <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>in</strong></span></strong></span> -fork mode- <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> 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 <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> process id 0
PM2        | 2016-12-28 18:14:26: Reloading logs <span style="color:#0000ff"><strong><span style="color:#0000ff"><strong>for</strong></span></strong></span> 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/<span style="color:#397300"><span style="color:#397300">test</span></span>-wb123-out-1.log last 10 lines:
/root/.pm2/logs/<span style="color:#397300"><span style="color:#397300">test</span></span>-wb123-error-1.log last 10 lines:
</code></span>

这个我们就能找到pm2中的日志,以及我们为输出的日志,这很重要,对于排除莫名其妙的问题!

进入我们的.pm2的文件中。

<span style="color:#000000"><code>[root@Zhaoqize 18:35 ~/.pm2]
<span style="color:#888888"><span style="color:#888888"># ls</span></span>
dump.pm2  ecosystem.config.js  logs  module_conf.json  pids  pm2.log  pm2.pid  pub.sock  rpc.sock  touch</code></span>
 类似资料: