Skip to content

PM2 使用指南

安装

shell
npm install pm2@latest -g
shell
yarn global add pm2

基础命令

进程管理

shell
# 基础启动
pm2 start app.js

# 指定应用名称
pm2 start app.js --name "myapp"

# 启用监视模式(文件变化时自动重启)
pm2 start app.js --watch

# 启动4个实例(集群模式)
pm2 start app.js -i 4

# 根据CPU核心数启动最大实例数
pm2 start app.js -i max
shell
# 硬重启
pm2 restart app.js

# 零停机重载(推荐用于生产环境)
pm2 reload app.js

# 优雅重载所有应用
pm2 gracefulReload all
shell
# 停止特定应用
pm2 stop app.js

# 停止所有应用
pm2 stop all
shell
# 删除特定应用
pm2 delete app.js

# 删除所有应用
pm2 delete all

监控和查看

shell
# 显示所有进程状态
pm2 list

# 等同于 list
pm2 status
shell
# 显示应用详细信息
pm2 show <app-name>

# 等同于 show
pm2 describe <app-name>
shell
# 监控所有进程资源使用情况
pm2 monit
shell
# 显示所有应用日志
pm2 logs

# 显示特定应用日志
pm2 logs <app-name>

# 显示最后200行日志
pm2 logs --lines 200

配置文件使用

配置文件示例

json
{
  "apps": {
    "name": "ws", // 应用名
    "script": "index.js", // 启动应用文件位置
    "cwd": "./", // 当前工作路径
    "watch": true, // 监听模式
    "ignore_watch": [
      // 忽略不想监听的文件改动
      "node_modules",
      "logs"
    ],
    "instances": 4, // 多进程,0表示根据CPU数量启动尽可能多的进程(集群模式)
    "error_file": "logs/err.log", // 普通输出日志文件及位置
    "out_file": "logs/out.log", // 错误输出日志文件及位置,使用pm2 install pm2-logrotate进行日志文件拆分
    "log_type": "json", // 日志类型
    "log_date_format": "YYYY-MM-DD HH:mm:ss" // 日志日期记录格式
  }
}

启动配置

shell
# 使用默认环境
pm2 start ecosystem.config.js
shell
# 使用生产环境配置
pm2 start ecosystem.config.js --env production

常用维护命令

shell
# 保存当前应用列表
pm2 save

# 热更新 PM2
pm2 update
shell
# 生成开机自启脚本
pm2 startup

# 保存当前应用列表以便开机启动
pm2 save
shell
# 清空所有日志
pm2 flush

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.