jieye の 数字花园

Search

Search IconIcon to open search

PM2

Last updated Nov 1, 2021

# PM2

# PM2

# 简介

对于线上项目,如果直接通过 node app 来启动,如果报错了可能直接停止导致整个服务崩溃,一般监控 node 有几种方案。

# PM2 的主要特性

# 安装

1
2
// 全局安装pm2,依赖node和npm
npm install -g pm2

# 常用命令

# PM2 Start
# 启动一个 Node 程序
1
2
3
4
5
pm2 start start.js
//Or start any other application easily:
$ pm2 start bashscript.sh
$ pm2 start python-app.py --watch
$ pm2 start binary-file -- --port 1520

# 启动进程并指定应用的程序名
1
pm2 start app.js --name application1
# 集群模式启动
1
2
3
// -i 表示 number-instances 实例数量
// max 表示 PM2将自动检测可用CPU的数量 可以自己指定数量
pm2 start start.js -i max

# 添加程监视
1
2
3
// 在文件改变的时候会重新启动程序
pm2 start app.js --name start --watch
复制代码

# 其他 Options
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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
# 列出所有进程
1
$ pm2 [list|ls|status]

# 重启、删除、停止、重新加载进程
1
2
3
4
$ pm2 restart app_name
$ pm2 reload app_name
$ pm2 stop app_name
$ pm2 delete app_name

除了使用 app_name,还可以:

# 查看状态、日志、指标
# 日志

To display logs in realtime:

1
$ pm2 logs

To dig in older logs:

1
$ pm2 logs --lines 200
# 自适应监控面板
1
$ pm2 monit

还有个 Web 版

1
$ pm2 plus

# 查看某个进程具体情况
1
pm2 describe app

img

# 设置 Pm2 开机自启

开启启动设置,此处是 CentOS 系统,其他系统替换最后一个选项(可选项:ubuntu, centos, redhat, gentoo, systemd, darwin, amazon)

1
pm2 startup centos 

然后按照提示需要输入的命令进行输入

最后保存设置

1
pm2 save
# 官方推荐命令
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Fork mode
pm2 start app.js --name my-api # Name process

# Cluster mode
pm2 start app.js -i 0        # Will start maximum processes with LB depending on available CPUs
pm2 start app.js -i max      # Same as above, but deprecated.
pm2 scale app +3             # Scales `app` up by 3 workers
pm2 scale app 2              # Scales `app` up or down to 2 workers total

# Listing

pm2 list               # Display all processes status
pm2 jlist              # Print process list in raw JSON
pm2 prettylist         # Print process list in beautified JSON

pm2 describe 0         # Display all informations about a specific process

pm2 monit              # Monitor all processes

# Logs

pm2 logs [--raw]       # Display all processes logs in streaming
pm2 flush              # Empty all log files
pm2 reloadLogs         # Reload all logs

# Actions

pm2 stop all           # Stop all processes
pm2 restart all        # Restart all processes

pm2 reload all         # Will 0s downtime reload (for NETWORKED apps)

pm2 stop 0             # Stop specific process id
pm2 restart 0          # Restart specific process id

pm2 delete 0           # Will remove process from pm2 list
pm2 delete all         # Will remove all processes from pm2 list

# Misc

pm2 reset <process>    # Reset meta data (restarted time...)
pm2 updatePM2          # Update in memory pm2
pm2 ping               # Ensure pm2 daemon has been launched
pm2 sendSignal SIGUSR2 my-app # Send system signal to script
pm2 start app.js --no-daemon
pm2 start app.js --no-vizion
pm2 start app.js --no-autorestart

# 管理多个应用

您还可以创建一个名为生态系统文件的配置文件来管理多个应用程序。生成生态系统文件:

1
$ pm2 ecosystem

生成 ecosystem.config.js 文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
module.exports = {
  apps : [{
    name: "app",
    script: "./app.js",
    env: {
      NODE_ENV: "development",
    },
    env_production: {
      NODE_ENV: "production",
    }
  }, {
     name: 'worker',
     script: 'worker.js'
  }]
}

And start it easily:

1
$ pm2 start process.yml

Read more about application declaration here.

# 通过 Pm2 配置文件来自动部署项目

官网指南

# 首先是配置服务器与 Github 的 Ssh
  1. 在服务器中生成 rsa 公钥和私钥,当前是 centos7 下进行

  2. 前提服务器要安装 git,没有安装的先安装 git,已安装的跳过

    1
    
    yum –y install git
    
  3. 生成秘钥

    1
    
    ssh-keygen -t rsa -C "xxx@xxx.com"
    

    在~/.ssh 目录下有 id_rsa 和 id_rsa.pub 两个文件,其中 id_rsa.pub 文件里存放的即是公钥 key。

  4. 登录到 GitHub,点击右上方的头像,选择 settings ,点击 Add SSH key,把 id_rsa.pub 的内容复制到里面即可。

# 本地项目 PM2 配置文件
1
pm2 ecosystem

在项目跟目录下运行,会自动生成模板文件:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
{
  // Applications part
  "apps" : [{
    "name"      : "API",
    "script"    : "app.js",
    "env": {
      "COMMON_VARIABLE": "true"
    },
    // Environment variables injected when starting with --env production
    // http://pm2.keymetrics.io/docs/usage/application-declaration/#switching-to-different-environments
    "env_production" : {
      "NODE_ENV": "production"
    }
  },{
    "name"      : "WEB",
    "script"    : "web.js"
  }],
  // Deployment part
  // Here you describe each environment
  "deploy" : {
    "production" : {
      // 服务器的用户名
      "user" : "node",
      // Multi host is possible, just by passing IPs/hostname as an array
      "host" : ["212.83.163.1", "212.83.163.2", "212.83.163.3"],
      // 要拉取的git分支
      "ref"  : "origin/master",
      // Git repository to clone
      "repo" : "git@github.com:repo.git",
      // 拉取到服务器某个目录下
      "path" : "/var/www/production",
      // Can be used to give options in the format used in the configura-
      // tion file.  This is useful for specifying options for which there
      // is no separate command-line flag, see 'man ssh'
      // can be either a single string or an array of strings
      "ssh_options": "StrictHostKeyChecking=no",
      // To prepare the host by installing required software (eg: git)
      // even before the setup process starts
      // can be multiple commands separated by the character ";"
      // or path to a script on your local machine
      "pre-setup" : "apt-get install git",
      // Commands / path to a script on the host machine
      // This will be executed on the host after cloning the repository
      // eg: placing configurations in the shared dir etc
      "post-setup": "ls -la",
      // Commands to execute locally (on the same machine you deploy things)
      // Can be multiple commands separated by the character ";"
      "pre-deploy-local" : "echo 'This is a local executed command'"
      // Commands to be executed on the server after the repo has been cloned
      "post-deploy" : "npm install && pm2 startOrRestart ecosystem.json --env production"
      // Environment variables that must be injected in all applications on this env
      "env"  : {
        "NODE_ENV": "production"
      }
    },
    "staging" : {
      "user" : "node",
      "host" : "212.83.163.1",
      "ref"  : "origin/master",
      "repo" : "git@github.com:repo.git",
      "path" : "/var/www/development",
      "ssh_options": ["StrictHostKeyChecking=no", "PasswordAuthentication=no"],
      "post-deploy" : "pm2 startOrRestart ecosystem.json --env dev",
      "env"  : {
        "NODE_ENV": "staging"
      }
    }
  }
}

关于 post-deploy

you may have noticed the command pm2 startOrRestart ecosystem.json --env production. The --env allows to inject different sets of environment variables.

Read more here.

按照自己要求修改好后,就可以部署啦:

1
pm2 deploy <configuration_file> <environment> setup

如:(windows 记得使用 git bash 等 unix 命令行)

1
pm2 deploy ecosystem.json production setup # 这个命令将会在远程服务器上创建文件
# Pm2 Deploy

pm2 deploy help:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
pm2 deploy <configuration_file> <environment> <command>

  Commands:
    setup                run remote setup commands
    update               update deploy to the latest release
    revert [n]           revert to [n]th last deployment or 1
    curr[ent]            output current release commit
    prev[ious]           output previous release commit
    exec|run <cmd>       execute the given <cmd>
    list                 list previous deploy commits
    [ref]                deploy to [ref], the "ref" setting, or latest tag