When starting a new process启动新流程时
PM2 will inject environment in this order when starting a new process :启动新流程时,PM2将按以下顺序注入环境:
First the PM2 CLI will use its environment so the current environment of your shell will be injected.首先,PM2 CLI将使用其环境,以便注入shell的当前环境。PM2 will then inject the environment that you can configure with the ecosystem file :然后,PM2将注入您可以使用生态系统文件配置的环境:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
env: {
"NODE_ENV": "development",
}
}
]
}
Here you can see that PM2 will override the current environment to add 在这里,您可以看到PM2将覆盖当前环境以添加NODE_ENV=development
. NODE_ENV=development
。But you can also define different environments like this :但您也可以这样定义不同的环境:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
env: {
"PORT": 3000,
"NODE_ENV": "development"
},
env_production: {
"PORT": 80,
"NODE_ENV": "production",
}
}
]
}
Here the default environment is in the 这里的默认环境位于env
, but you can decide to use env_production
by using pm2 start ecosystem.config.js --env production
.env
中,但您可以通过使用pm2 start ecosystem.config.js --env production
来决定使用env_production
。
You can define as many environments as you like, just remember that you must pass the name of the environment (after 您可以定义任意多个环境,只需记住您必须传递要与env_
) you want to use with --env
.--env
一起使用的环境的名称(在env_
之后)。
Specific environment variables特定环境变量
NODE_APP_INSTANCE (PM2 2.5 minimum)
There is the 有一个NODE_APP_INSTANCE
environment variable that is used to make a difference between process, for example you may want to run a cronjob only on one process, you can just check if process.env.NODE_APP_INSTANCE === '0'
. NODE_APP_INSTANCE
环境变量用于区分进程,例如,您可能只想在一个进程上运行cronjob,您只需检查process.env.NODE_APP_INSTANCE === '0'
。Two processes can never have the same number, its still true after 两个进程不可能有相同的数字,在pm2 restart
and pm2 scale
commands.pm2 restart
和pm2 scale
命令后仍然如此。
You may have problems with node-config with the 使用NODE_APP_INSTANCE
name, so you can rename it with instance_var
options :NODE_APP_INSTANCE
名称的node-config可能有问题,因此可以使用instance_var
选项重命名它:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
watch: true,
instance_var: 'INSTANCE_ID',
env: {
"PORT": 3000,
"NODE_ENV": "development"
}
}
]
}
In this case the variable will have the same behavior but will be in 在这种情况下,变量将具有相同的行为,但将位于process.env.INSTANCE_ID
.process.env.INSTANCE_ID
中。
increment_var (PM2 2.5 minimum)
There is an option to ask PM2 to increment a environment variable for each instance launched, for example:有一个选项要求PM2为每个启动的实例增加一个环境变量,例如:
module.exports = {
apps : [
{
name: "myapp",
script: "./app.js",
instances: 2,
exec_mode: "cluster",
watch: true,
increment_var : 'PORT',
env: {
"PORT": 3000,
"NODE_ENV": "development"
}
}
]
}
In this example, if i run 在本例中,如果我运行pm2 start ecosystem.config.js
:pm2 start ecosystem.config.js
:
PM2 will see that i want to increment thePM2将看到我想要为每个实例增加PORT
variable for each instancePORT
变量It will see that i have defined the default to它将看到我已将默认值定义为3000
3000
The first instance will have第一个实例的process.env.PORT = 3000
and the secondprocess.env.PORT = 3001
process.env.PORT=3000
,第二个实例的process.env.PORT=3001
NOTE : It will increment also when scaling using 注意:当使用pm2 scale myapp 4
, both new instances will have 3002
and 3003
as PORT
variable.pm2 scale myapp 4
进行缩放时,它也会增加,两个新实例都将3002
和3003
作为PORT
变量。