Configuration File配置文件

When managing multiple applications with PM2, use a JS configuration file to organize them.使用PM2管理多个应用程序时,请使用JS配置文件来组织它们。

Generate configuration生成配置

To generate a sample configuration file you can type this command:要生成示例配置文件,可以键入以下命令:

$ pm2 init simple

This will generate a sample ecosystem.config.js:这将生成一个示例ecosystem.config.js

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

If you are creating your own configuration file, make sure it ends with .config.js so PM2 is able to recognize it as a configuration file.如果您正在创建自己的配置文件,请确保它以.config.js结尾,以便PM2能够将其识别为配置文件。

Acting on Configuration File作用于配置文件

Seamlessly than acting on an app you can start/stop/restart/delete all apps contained in a configuration file:与对应用程序执行操作相比,您可以无缝地启动/停止/重新启动/删除配置文件中包含的所有应用程序:

# Start all applications启动所有应用程序
pm2 start ecosystem.config.js

# Stop all停止所有应用程序
pm2 stop ecosystem.config.js

# Restart all重启所有应用程序
pm2 restart ecosystem.config.js

# Reload all重载所有应用程序
pm2 reload ecosystem.config.js

# Delete all删除所有应用程序
pm2 delete ecosystem.config.js

Act on a specific process按特定过程行事

You can also act on a particular application by using its name and the option --only <app_name>:您还可以使用特定应用程序的名称和选项--only <app_name>

pm2 start   ecosystem.config.js --only api-app

Note: the --only option works for start/restart/stop/delete as well注意--only选项也适用于启动/重新启动/停止/删除

You can even specify multiple apps to be acted on by specifying each app name separated by a comma:您甚至可以通过指定每个应用程序名(以逗号分隔)来指定要操作的多个应用程序:

pm2 start ecosystem.config.js --only "api-app,worker-app"

Switching environments交换环境

You can specify different environment variable set via the env_* option.可以通过env_*选项指定不同的环境变量集。

Example:例子:

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

Now to switch between variables in different environment, specify the --env [env name] option:现在,要在不同环境中的变量之间切换,请指定--env [env name]选项:

pm2 start process.json --env production
pm2 restart process.json --env development

Attributes available可用属性

Application behavior and configuration can be fine-tuned with the following attributes:可以使用以下属性微调应用程序行为和配置:

General常规

Field字段 Type类型 Example例子 Description描述
name (string) "my-api" application name (default to script filename without extension)应用程序名称(默认为不带扩展名的脚本文件名)
script (string) "./api/app.js" script path relative to pm2 start相对于pm2启动的脚本路径
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包含通过CLI传递给脚本的所有参数的字符串
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_argsinterpreter_args的别名

Advanced features高级功能

Field字段 Type类型 Example例子 Description描述
instances number -1 number of app instance to be launched要启动的应用程序实例数
exec_mode string "cluster" mode to start your app, can be “cluster” or “fork”, default fork启动应用程序的模式,可以是“cluster(群集)”或“分叉”,默认fork(分叉)
watch boolean or [] true enable watch & restart feature, if a file change in the folder or subfolder, your app will get reloaded启用监视并重新启动功能,如果文件夹或子文件夹中的文件发生更改,您的应用程序将重新加载
ignore_watch list ["[\/\\]\./", "node_modules"] list of regex to ignore some file or folder names by the watch feature通过监视功能忽略某些文件或文件夹名称的正则表达式列表
max_memory_restart string "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…人性化格式:可以是“10M”、“100K”、“2G”等
env object {"NODE_ENV": "development", "ID": "42"} env variables which will appear in your app将显示在应用程序中的环境变量
env_<env_name></env_name> object {"NODE_ENV": "production", "ID": "89"} inject <env_name> when doing pm2 restart app.yml --env <env_name></env_name></env_name>当执行pm2 restart app.yml --env <env_name></env_name></env_name>时注入<env_name>
source_map_support boolean true default to true, [enable/disable source map file]默认为true,[启用/禁用源映射文件]
instance_var string "NODE_APP_INSTANCE" see documentation请参阅文档
filter_env array of string ["REACT_"] Excludes global variables starting with “REACT_” and will not allow their penetration into the cluster.排除以“REACT_”开头的全局变量,并且不允许它们渗透到集群中。

Log files日志文件

Field字段 Type类型 Example例子 Description描述
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)错误文件路径(默认为$HOME/.pm2/logs/XXXerr.log
out_file (string)   output file path (default to $HOME/.pm2/logs/XXXout.log)输出文件路径(默认为$HOME/.pm2/logs/XXXout.log
combine_logs boolean true if set to true, avoid to suffix logs file with the process id如果设置为true,请避免在日志文件中添加进程id后缀
merge_logs boolean true alias to combine_logscombine_logs的别名
pid_file (string)   pid file path (default to $HOME/.pm2/pid/app-pm_id.pid)pid文件路径(默认为$HOME/.pm2/pid/app-pm_id.pid

Control flow控制流

Field字段 Type类型 Example例子 Description描述
min_uptime (string)   min uptime of the app to be considered started要考虑启动的应用程序的最小正常运行时间
listen_timeout number 8000 time in ms before forcing a reload if app not listening如果应用程序未侦听,则强制重新加载之前的时间(毫秒)
kill_timeout number 1600 time in milliseconds before sending a final SIGKILL发送最终SIGKILL之前的时间(毫秒)
shutdown_with_message boolean false shutdown an application with process.send('shutdown') instead of process.kill(pid, SIGINT)使用process.send('shutdown')而不是process.kill(pid, SIGINT)关闭应用程序
wait_ready boolean false Instead of reload waiting for listen event, wait for process.send('ready')等待process.send('ready'),而不是重新加载等待侦听事件
max_restarts number 10 number of consecutive unstable restarts (less than 1sec interval or custom time via min_uptime) before your app is considered errored and stop being restarted在您的应用被视为错误并停止重新启动之前,连续不稳定的重新启动次数(小于1秒的间隔或通过min_uptime的自定义时间)
restart_delay number 4000 time to wait before restarting a crashed app (in milliseconds). defaults to 0.重新启动崩溃的应用程序之前的等待时间(以毫秒为单位)。默认值为0
autorestart boolean false true by default. 默认情况下为trueif false, PM2 will not restart your app if it crashes or ends peacefully如果为false,PM2将不会在应用程序崩溃或和平结束时重新启动应用程序
cron_restart string "1 0 * * *" a cron pattern to restart your app. 用于重新启动应用程序的cron模式。Application must be running for cron feature to work应用程序必须运行才能使cron功能正常工作
vizion boolean false true by default. 默认情况下为trueif false, PM2 will start without vizion features (versioning control metadatas)如果为false,PM2将在没有vizion功能(版本控制元数据)的情况下启动
post_update后更新 list ["npm install", "echo launching the app"] a list of commands which will be executed after you perform a Pull/Upgrade operation from Keymetrics dashboard从Keymetrics仪表板执行拉入/升级操作后将执行的命令列表
force boolean true defaults to false. if true, you can start the same script several times which is usually not allowed by PM2默认为false。如果为true,则可以多次启动同一脚本,这通常是PM2不允许的

Deployment部署

Entry name条目名称 Description描述 Type类型 Default默认值
key SSH key pathSSH密钥路径 String $HOME/.ssh
user SSH userSSH用户 String  
host SSH hostSSH主机 [String]  
ssh_options SSH options with no command-line flag, see ‘man ssh’没有命令行标志的SSH选项,请参阅“man ssh” String or [String]  
ref GIT remote/branchGIT远程/分支 String  
repo GIT remoteGIT远程 String  
path path in the server服务器中的路径 String  
pre-setup Pre-setup command or path to a script on your local machine本地计算机上脚本的预设置命令或路径 String  
post-setup Post-setup commands or path to a script on the host machine在主机上发布脚本的设置命令或路径 String  
pre-deploy-local pre-deploy action部署前行动 String  
post-deploy post-deploy action部署后行动 String  

Considerations考虑事项

All command line options passed when using the JSON app declaration will be dropped i.e.使用JSON应用程序声明时传递的所有命令行选项都将被删除,即。

CWD

cwd: your JSON declaration does not need to reside with your script. JSON声明不需要与脚本一起驻留。If you wish to maintain the JSON(s) in a location other than your script (say, /etc/pm2/conf.d/node-app.json) you will need to use the cwd feature (Note, this can be really helpful for capistrano style directory structures that uses symlinks). 如果您希望在脚本以外的位置(比如/etc/pm2/conf.d/node-app.json)维护JSON,则需要使用cwd功能(注意,这对于使用符号链接的capistrano样式的目录结构非常有用)。Files can be either relative to the cwd directory, or absolute (see example below).文件可以是相对于cwd目录的,也可以是绝对的(参见下面的示例)。