1. 安装pm2

npm install pm2 -g

yarn global add pm2

使用pm2 -v查看版本号

2. 基本使用

2.1 启动应用

pm2 start app.js # 不止是js文件,其他可执行文件也可以执行

pm2 start script.sh # 启动bash脚本

pm2 start python3 -- app.py  # -- 后跟要传给命令的参数

pm2 start binary -- -port 8080

在启动应用时还有一些参数

--name <app_name> # 为应用设置一个名字

--watch # 监视源文件并在源文件存在更改时重启应用

--max-memory-restart <200MB> # 设置应用占用内存上限

--log <log_path> # 设置log文件路径

-- arg1 arg2 arg3 # 传递参数

--restart-delay <delay in ms>  # 重启前延时

--time 在日志前增加时间戳

--no-autorestart # 不要自动重启

2.2 管理应用

pm2 restart app_name 

pm2 reload app_name

pm2 stop app_name

pm2 delete app_name

除了使用app_name,还可以使用应用的id, 或使用all来批量管理所有应用

2.3 列出被管理的应用

pm2 list|ls|status

20210806191943

2.4 打印log

pm2 log 

打印最后两百行:

pm2 log --line 200

2.5 显示监控

pm2 monit

20210806192323

3. ecosystems.config.js – 使用配置文件启动应用

3.1 基本使用

生成一个示例文件

pm2 init simple

20210806192510

生成的示例文件如下:

module.exports = {
  apps : [{
    name   : "app1",
    script : "./app.js"
  }]
}

上面的配置文件中定义了一个最简单的应用,其nameapp1, 启动方式为./app.js

管理应用:

pm2 start ecosystem.config.js # 启动应用

pm2 stop ecosystem.config.js # 停止应用

pm2 restart ecosystem.config.js # 重启应用

pm2 reload ecosystem.config.js # 重新加载应用

pm2 delete ecosystem.config.js # 删除应用

如果你的配置文件中配置了多个文件,那他看起来可能长这样:

module.exports = {
  apps : [
    {
        name   : "app1",
        script : "./app1.js"
    },
    {
        name: "app2",
        script : "./app2.js"
    },
    {
        name: "app3",
        script : "./app3.js"
    },
    {
        name: "app4",
        script : "./app4.js"
    }
  ]
}

如果想要单独管理配置文件中的某(几)个应用,可以使用--only参数

pm2 stop ecosystem.config.js --only app1 # 只停止app1

pm2 delete ecosystem.config.js --only "app1,app2"  # 只删除app1和app2

当然你还可以用他们的id来管理

3.2 配置不同环境

可能你有开发环境和生产环境两个环境,而二者在启动时可能需要不同的环境变量(如数据库节点),那么你可以为每个应用配置env_*

module.exports = {
  apps : [
    {
        name   : "app1",
        script : "./app.js",
        env_production: {
            NODE_ENV: "production",
            SQL_ENDPOINT: "192.168.11.11",
        },
        env_development: {
            NODE_ENV: "development",
            SQL_ENDPOINT: "192.168.22.22",
        }
    }
  ]
}

在这个配置文件中, 定义了两套环境变量,你可以选择其中之一用来启动你的应用

pm2 start ecosystem.config.js --env production # 使用生产环境的环境变量,即:SQL_ENDPOINT = "192.168.11.11"

pm2 start ecosystem.config.js --env development # 使用开发环境的环境变量,即:SQL_ENDPOINT = "192.168.22.22"

3.3 其他参数

基本参数

FieldTypeExampleDescription
name(string)“my-api”application name (default to script filename without extension)
script(string)”./api/app.js”script path relative to pm2 start
cwd(string)“/var/www/”the directory from which your app will be launched
args(string)“-a 13 -b 12”string containing all arguments passed via CLI to script
interpreter(string)“/usr/bin/python”interpreter absolute path (default to node)
interpreter_args(string)”–harmony”option to pass to the interpreter
node_args(string)alias to interpreter_args

高级参数

FieldTypeExampleDescription
instancesnumber-1number of app instance to be launched
exec_modestring“cluster”mode to start your app, can be “cluster” or “fork”, default fork
watchboolean or []trueenable watch & restart feature, if a file change in the folder or subfolder, your app will get reloaded
ignore_watchlist[”[/\]./”, “node_modules”]list of regex to ignore some file or folder names by the watch feature
max_memory_restartstring“150M”your app will be restarted if it exceeds the amount of memory specified. human-friendly format : it can be “10M”, “100K”, “2G” and so on…
envobject{“NODE_ENV”: “development”, “ID”: “42”}env variables which will appear in your app
env_object{“NODE_ENV”: “production”, “ID”: “89”}inject when doing pm2 restart app.yml –env
source_map_supportbooleantruedefault to true, [enable/disable source map file]
instance_varstring“NODE_APP_INSTANCE”see documentation
filter_envarray of string[ “REACT_” ]Excludes global variables starting with “REACT_” and will not allow their penetration into the cluster.

log配置

FieldTypeExampleDescription
log_date_format(string)“YYYY-MM-DD HH:mm Z”log date format (see log section)
error_file(string)error file path (default to $HOME/.pm2/logs/XXXerr.log)
out_file(string)output file path (default to $HOME/.pm2/logs/XXXout.log)
combine_logsbooleantrueif set to true, avoid to suffix logs file with the process id
merge_logsbooleantruealias to combine_logs
pid_file(string)pid file path (default to $HOME/.pm2/pid/app-pm_id.pid)

控制流参数

FieldTypeExampleDescription
min_uptime(string)min uptime of the app to be considered started
listen_timeoutnumber8000time in ms before forcing a reload if app not listening
kill_timeoutnumber1600time in milliseconds before sending a final SIGKILL
shutdown_with_messagebooleanfalseshutdown an application with process.send(‘shutdown’) instead of process.kill(pid, SIGINT)
wait_readybooleanfalseInstead of reload waiting for listen event, wait for process.send(‘ready’)
max_restartsnumber10number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being restarted
restart_delaynumber4000time to wait before restarting a crashed app (in milliseconds). defaults to 0.
autorestartbooleanfalsetrue by default. if false, PM2 will not restart your app if it crashes or ends peacefully
cron_restartstring“1 0 * * *”a cron pattern to restart your app. Application must be running for cron feature to work
vizionbooleanfalsetrue by default. if false, PM2 will start without vizion features (versioning control metadatas)
post_updatelist[“npm install”, “echo launching the app”]a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard
forcebooleantruedefaults to false. if true, you can start the same script several times which is usually not allowed by PM2

部署相关参数

Entry nameDescriptionTypeDefault
keySSH key pathString$HOME/.ssh
userSSH userString
hostSSH host[String]
ssh_optionsSSH options with no command-line flag, see ‘man ssh’String or [String]
refGIT remote/branchString
repoGIT remoteString
pathpath in the serverString
pre-setupPre-setup command or path to a script on your local machineString
post-setupPost-setup commands or path to a script on the host machineString
pre-deploy-localpre-deploy actionString
post-deploypost-deploy actionString

References

https://pm2.keymetrics.io/