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常规
name |
(string) | "my-api" |
|
script |
(string) | "./api/app.js" |
|
cwd |
(string) | "/var/www/" |
|
args |
(string) | "-a 13 -b 12" |
|
interpreter |
(string) | "/usr/bin/python" |
|
interpreter_args |
(string) | "–harmony" |
|
node_args |
(string) | interpreter_args 的别名 |
Advanced features高级功能
instances |
number | -1 |
|
exec_mode |
string | "cluster" |
|
watch |
boolean or [] | true |
|
ignore_watch |
list | ["[\/\\]\./", "node_modules"] |
|
max_memory_restart |
string | "150M" |
|
env |
object | {"NODE_ENV": "development", "ID": "42"} |
|
env_<env_name></env_name> |
object | {"NODE_ENV": "production", "ID": "89"} |
pm2 restart app.yml --env <env_name></env_name></env_name> 时注入<env_name> |
source_map_support |
boolean | true |
true ,[启用/禁用源映射文件] |
instance_var |
string | "NODE_APP_INSTANCE" |
|
filter_env |
array of string | ["REACT_"] |
REACT_ ”开头的全局变量,并且不允许它们渗透到集群中。 |
Log files日志文件
log_date_format |
(string) | "YYYY-MM-DD HH:mm Z" |
|
error_file |
(string) | $HOME/.pm2/logs/XXXerr.log ) |
|
out_file |
(string) | $HOME/.pm2/logs/XXXout.log ) |
|
combine_logs |
boolean | true |
true ,请避免在日志文件中添加进程id后缀 |
merge_logs |
boolean | true |
combine_logs 的别名 |
pid_file |
(string) | $HOME/.pm2/pid/app-pm_id.pid ) |
Control flow控制流
min_uptime |
(string) | ||
listen_timeout |
number | 8000 |
|
kill_timeout |
number | 1600 |
|
shutdown_with_message |
boolean | false |
process.send('shutdown') 而不是process.kill(pid, SIGINT) 关闭应用程序 |
wait_ready |
boolean | false |
process.send('ready') ,而不是重新加载等待侦听事件 |
max_restarts |
number | 10 |
min_uptime 的自定义时间) |
restart_delay |
number | 4000 |
0 。 |
autorestart |
boolean | false |
true 。false ,PM2将不会在应用程序崩溃或和平结束时重新启动应用程序 |
cron_restart |
string | "1 0 * * *" |
|
vizion |
boolean | false |
true 。false ,PM2将在没有vizion 功能(版本控制元数据)的情况下启动 |
list | ["npm install", "echo launching the app"] |
||
force |
boolean | true |
false 。如果为true ,则可以多次启动同一脚本,这通常是PM2不允许的 |
Deployment部署
key |
String | $HOME/.ssh | |
user |
String | ||
host |
[String] | ||
ssh_options |
String or [String] | ||
ref |
String | ||
repo |
String | ||
path |
String | ||
pre-setup |
String | ||
post-setup |
String | ||
pre-deploy-local |
String | ||
post-deploy |
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
目录的,也可以是绝对的(参见下面的示例)。