当前位置:网站首页>PM2 configuration file

PM2 configuration file

2022-08-09 11:45:00 choukin

There are three of the most harmful addictive substances in the world:海洛因、碳水化合物、Salary paid once a month.————《黑天鹅》Author Taleb

当使用PM2When managing multiple apps,可以使用 JS profiles to manage them.

生成配置文件

Use the command below to generate a simple configuration file:

pm2 init simple

会生成一个简单的 ecosystem.config.js:


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

If a custom configuration file is created,Make sure the filename is .config.js 结尾的,这样 PM2 It can be managed as a configuration file.

操作配置文件

compared to performing an action on an application,You can use the configuration file directly 启动/停止/重新启动/删除 All applications in the configuration file:

# 启动所有应用程序
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

Operates the specified process

可以使用--only <app_name>Operate a specific application:

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

注意:--only The choice can also be right start/restart/stop/delete 操作.

It can also be separated by commas,Operates multiple specific applications:

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

切换环境

可以通过设置 env_* to set different environment variables:

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

可以通过 --env [env name] to switch environment variables:

pm2 start ecosystem.config.js --env production

pm2 restart ecosystem.config.js --env development

可用属性

The behavior and configuration of the application can be fine-tuned through the properties below:

常用

字段类型例子描述
namestring"my-api"应用名称,The default is the script name excluding the suffix
scriptstring"./api/app.js"相对于pm2的脚本路径
cwdstring"/var/www/"The directory where the application is started
argsstring"-a 13 -b 12"包含所有的参数,These parameters can be passed to the script via the command line
interpreterstring"/usr/bin/python"解释器的绝对路径,默认是nodejs的
interpreter_argsstring"-harmony"Arguments passed to the interpreter
node_argsstringInterpreter parameter aliases

高级功能

字段类型例子描述
instancesnumber-1The number of samples to start the application with
exec_modestring"cluster"The mode in which to start the application,“cluster”/ "fork" 默认是 fork
watchboolean / []trueEnables monitoring and restarting if the folder or files in the folder are changed,The program will reload
ignore_watch[]["node_modules","[/\]./"]A series of regular or folder names,Indicates the folder to ignore
max_mermory_restartstring“150M”If the application exceeds the specified memory,The application will restart.
envobject{"NODE_ENV":"development","ID":"42"}Environment variables used in the application
env_object{"NODE_ENV":"production","ID":"89"}在pm2Injected when the app is restarted pm2 restart ecosystem.config.js -env
source_map_supportbooleantrue默认是true[启用/禁用 source map 文件]
instance_varstring"NODE_APP_INSTANCE"
filter_env字符串数组["REACT_"]excluded"REACT_"开头的全局变量,They are not allowed to infiltrate the cluster

日志文件

字段类型例子描述
log_date_formatstring'YYYY-MM-DD HH:mm Z'
erro_filestring错误日志文件 默认 $HOME/.pm2/logs/XXXerr.log
out_filestring日志文件 默认 $HOME/.pm2/logs/XXXout.log
combine_logsbooleantrue如果设置true,Avoid adding processes to log filesid后缀
merge_logsbooleantruecombine_logs 别名
pid_filestring进程id文件路径 $HOME/.pm2/pid/app-pm_id.pid

控制流

字段类型例子描述
min_updtimestringControls the minimum application startup time
listen_timeoutnumber8000The timeout if the application is not being listened to,If it exceeds it will reload 毫秒
kill_timeoutnumber1600发送killnumber of milliseconds before
shutdown_with_messagenubmerfalseprocess.send('shutdown')而不是process.kill(pid, SIGINT) to close the application
wait_readybooleanfalse等待process.send('ready')instead of waiting for the reload event
max_restartsnumber10Before the app is considered an error and stops restarting,The number of consecutive unstable restarts(Less than one second interval or passmin_uptimecustom time)
restart_delaynumber4000Default wait time before restarting a crashed app0
autorestartbooleanfalse默认true,如果设置false,PM2 No restart on app crash or calm end
cron_restartstring“1 0 * * *”Restart the application cron表达式, 应用必须在cronfunction is available
vizionbooleanfalse默认true,如果false PM2 Not included after startupvizion功能(Control metadata version)
post_updatelist["npm install", "echo launching the app"]从KeymetricsDashboard executionpull/upgrade A series of commands to execute during operation
forcebooleantrue默认false,如果设置为true,The same script can be launched multiple times,这通常是PM2 不允许的.

部署

字段描述类型默认值
keySSH key 路径stirng$HOME/.ssh
userSSH 用户名string
hostSSH HOST[stirng]
ssh_optionswithout command line flagsSSH选项,参阅 man sshStirng / [String]
refGIT 远程分支 remote/branchString
repoGIT 远程仓库String
path服务器路径
pre-setupcommand before installation,or local script pathString
post-setup安装后的命令,Or the server script pathString
pre-deploy-localActions before deploymentString
post-deployPost-deployment actionsString
原网站

版权声明
本文为[choukin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091131168110.html