> For the complete documentation index, see [llms.txt](https://1425816423.gitbook.io/my-knowledge-base/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://1425816423.gitbook.io/my-knowledge-base/qian-duan-ji-shu/qian-duan-gong-cheng-hua/pm2.md).

# pm2

## PM2

### 介绍

PM2 是一个守护进程管理器，可帮助您管理和保持应用程序在线。pm2提供了很多实用的功能，例如日志、性能监控、自动重启、负载均衡等。

安装

```
$ npm install pm2@latest -g
```

更新

```
$ npm install pm2@latest -g
$ pm2 update
```

### 进程管理

```
// 启动
$ pm2 start app.js
// 重启
$ pm2 restart app_name
// 重新加载
$ pm2 reload app_name
// 停止
$ pm2 stop app_name
// 删除
$ pm2 delete app_name
```

app\_name可以是项目name，也可以是id。

### 启动参数

`pm2 start`时可以进行参数配置，配置参数如下

```
# Specify an app name
--name <app_name>

# Watch and Restart app when files change
--watch

# Set memory threshold for app reload
--max-memory-restart <200MB>

# Specify log file
--log <log_path>

# Pass extra arguments to the script
-- arg1 arg2 arg3

# Delay between automatic restarts
--restart-delay <delay in ms>

# Prefix logs with time
--time

# Do not auto restart app
--no-autorestart

# Specify cron for forced restart
--cron <cron_pattern>

# Attach to application log
--no-daemon
```

### 查看状态

以下指令都可以查看pm2当前所有项目进程的状态列表。

```
$ pm2 list
$ pm2 status
$ pm2 ls
$ pm2 l
```

输出如下

```
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name        │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ blog_end    │ default     │ 0.37.2  │ fork    │ 24594    │ 16m    │ 0    │ online    │ 0%       │ 32.8mb   │ root     │ disabled │
└─────┴─────────────┴────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
```

### 查看日志

实时显示日志

```
$ pm2 log
```

查看历史日志（最近的200行）

```
$ pm2 log --lines 200
```

查看特定项目的日志

```
$ pm2 log app_name/app_id
```

只看错误日志

```
$ pm2 log --err app_name/app_id
```

清空所有日志文件

```
$ pm2 flush
```

重新加载所有日志文件

```
$ pm2 reloadLogs
```

### 调试

pm2可视化面板，以可视化的界面查看项目实时log、负载等信息。

```
$ pm2 monit
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://1425816423.gitbook.io/my-knowledge-base/qian-duan-ji-shu/qian-duan-gong-cheng-hua/pm2.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.
