# PM2 常用命令与监控

#### 启动

```
$ pm2 start app.js               # 启动app.js应用程序
$ pm2 start app.js -i 4          # cluster mode 模式启动4个app.js的应用实例      # 4个应用程序会自动进行负载均衡
$ pm2 start app.js --name="api"  # 启动应用程序并命名为 "api"
$ pm2 start app.js --watch       # 实时监控app.js的方式启动，当app.js文件有变动时，pm2会自动reload
$ pm2 start script.sh            # 启动 bash 脚本
```

注意：

> -i --instances：启用多少个实例，可用于负载均衡。如果-i 0或者-i max，则根据当前机器核数确定实例数目。
>
> 查看cpu核数参考：[Linux查看物理CPU个数、核数、逻辑CPU个数](https://www.cnblogs.com/bugutian/p/6138880.html)
>
> ```
> cat /proc/cpuinfo| grep "cpu cores"| uniq
> ```

#### 重启

```
$ pm2 restart all               # 重启所有应用
```

#### 重载

```
$ pm2 reload all                # 重启 cluster mode下的所有应用
```

#### 停止

```
$ pm2 stop all                  # 停止所有的应用程序
$ pm2 stop 0                    # 停止 id为 0的指定应用程序
```

#### 查看进程

```
$ pm2 list # 列表 PM2 启动的所有的应用程序
$ pm2 show [app-name或 id] # 显示应用程序的所有信息
$ pm2 info [app-name或 id] # 显示应用程序的所有信息
```

#### 实时监控

```
$ pm2 monit          # 显示每个应用程序的CPU和内存占用情况
$ pm2 monit 0          # 监控批评行编号为0的进程
$ pm2 monit server.js  # 监控名称为server.js的进程
```

#### 日志

```
$ pm2 logs                   # 显示所有应用程序的日志
$ pm2 logs [app-name或 id]    # 显示指定应用程序的日志
$ pm2 flush                  #Empty all log file
$ pm2 reloadLogs             #Reload all logs
```

#### 删除

```
$ pm2 delete all                # 关闭并删除所有应用
$ pm2 delete 0                  # 删除指定应用 id 0
```

#### 升级PM2

```
$ npm install pm2@lastest -g   #安装最新的PM2版本
$ pm2 update                   #升级pm2
```

#### 其他

```
$ pm2 gracefulReload all        # Graceful reload all apps in cluster mode(优雅地重新加载集群模式中的所有应用程序)
$ pm2 scale api 10              # 把名字叫api的应用扩展到10个实例
$ pm2 reset [app-name]          # 重置重启数量
$ pm2 startup                   # 创建开机自启动命令
$ pm2 save                      # 保存当前应用列表
$ pm2 resurrect                 # 重新加载保存的应用列表
$ pm2 update                    # Save processes, kill PM2 and restore processes
$ pm2 generate                  # Generate a sample json configuration file
```

## 开机自动启动 <a href="#kai-ji-zi-dong-qi-dong" id="kai-ji-zi-dong-qi-dong"></a>

可以通过`pm2 startup`来实现开机自启动。细节可[参考](http://pm2.keymetrics.io/docs/usage/startup/)。大致流程如下

1. 通过`pm2 save`保存当前进程状态。
2. 通过`pm2 startup [platform]`生成开机自启动的命令。（记得查看控制台输出）
3. 将步骤2生成的命令，粘贴到控制台进行，搞定。

### 资料

脚本方式可以参考资料: [pm2 相关命令](http://blog.csdn.net/dlmmu/article/details/55683771)

[pm2常用的命令用法介绍](https://blog.csdn.net/chengxuyuanyonghu/article/details/74910875)

[生成配置文件](http://pm2.keymetrics.io/docs/usage/application-declaration/#javascript-format)

### 示例

```
git clone http://xxxx.git
cd xxx-service
git checkout tags/v1.0.0 -b v1.0.0
npm install
NODE_ENV=production pm2 start server.js --name="xxx service"
pm2 save
```

```
PORT=3003 NODE_ENV=development pm2 start index.js --name="salse-service:3003"
NODE_ENV=production PORT=3030 pm2 start index.js --name="Salesoffice service:3030"
```

[PM2实用入门指南](https://legacy.gitbook.com/book/xiaoxiami/nodejs/edit)

### pm2-web监控

安装

```
sudo npm install -g pm2-web
```

运行

在pm2启动的情况下，键入以下指令：

```
pm2-web
```

浏览器浏览即可，默认是9000端口

也可以进行配置

```
$ mkdir /etc/pm2-web/
$ cd /etc/pm2-web/
$ sudo vi config.json
```

改变端口

```javascript
{
   "www": {
       "host": "127.0.0.1",
        "address": "0.0.0.0",
        "port": 10016
   }
}
```

默认配置地址：

<https://github.com/achingbrain/pm2-web/blob/master/config.json>

#### 资料

[安装 pm2-web 出错](https://www.jianshu.com/p/accfea290116)

[nodejs+pm2+pm2-web简说](https://www.jianshu.com/p/9cb24de7c7dd)

<https://blog.csdn.net/dongshaoshuai/article/details/50292937>[使用 pm2-web 监控 pm2 服务运行状态](https://blog.csdn.net/dongshaoshuai/article/details/50292937)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/nodejs/pm2-chang-yong-ming-ling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
