I have an Upstart script for my server that looks like this:
我的服务器的Upstart脚本如下所示:
description "myapp node.js server"
start on runlevel [2345]
stop on shutdown
env NODE_ENV=production
env CUSTOM=somevalue
exec sudo -u nodejs /usr/local/bin/node /opt/myapp/app.js >> /var/log/nodejs/myapp.log 2>&1
post-start script
NODE_PID=`status myapp | egrep -oi '([0-9]+)$' | head -n1`
echo $NODE_PID > /var/run/nodejs/myapp.pid
end script
However, the app doesn't see NODE_ENV set to production. In fact, if I console.log(process.env) within the app, I don't see NODE_ENV or CUSTOM. Any ideas what's happening?
但是,该应用程序未将NODE_ENV设置为生产。事实上,如果我在app中使用console.log(process.env),我看不到NODE_ENV或CUSTOM。有什么想法发生了什么?
By the way, NODE_ENV=production node app.js works just fine.
顺便说一句,NODE_ENV =生产节点app.js工作正常。
4 个解决方案
#1
38
From the sudo man page (Ubuntu version of sudo)
来自sudo手册页(Ubuntu版的sudo)
There are two distinct ways to deal with environment variables. By default, the env_reset sudoers option is enabled. This causes commands to be executed with a minimal environment containing TERM, PATH, HOME, SHELL, LOGNAME, USER and USERNAME in addition to variables from the invoking process permitted by the env_check and env_keep sudoers options. There is effectively a whitelist for environment variables.
有两种不同的方法来处理环境变量。默认情况下,启用env_reset sudoers选项。这将导致与含有TERM,PATH,HOME,SHELL,LOGNAME,除了从由env_check和env_keep sudoers的选项允许调用过程变量USER和USERNAME一个最小的环境中执行的命令。环境变量实际上是白名单。
Sudo is resetting the environment. This is a frustrating aspect of using su
and sudo
in upstart or init scripts. Recent versions of upstart support specifying uid/gid without the use of sudo via the setuid/setgid
directives as in the example below. Also note the use of chdir
.
Sudo正在重置环境。在upstart或init脚本中使用su和sudo是一个令人沮丧的方面。最新版本的upstart支持通过setuid / setgid指令指定uid / gid而不使用sudo,如下例所示。还要注意chdir的使用。
start on filesystem and started networking
respawn
chdir /var/www/yourapp
setuid yourapp
setgid yourapp
env NODE_ENV=production
env PATH=/usr/local/bin:/usr/bin:/bin
env CUSTOM=somevalue
exec /usr/local/bin/node app.js | /usr/bin/multilog s1024000 /var/log/yourapp 2>&1
For older versions of upstart, here's what I used to do to work around it.
对于旧版本的新贵,这是我过去常常要解决的问题。
description "start and stop the example.com node.js server"
start on filesystem and started networking
respawn
chdir /path/to/your/code
exec su -c 'PATH=$PWD/node/bin NODE_ENV=$(cat node_env.txt) ./node/bin/node app/server.js' www-data >> tmp/stdout.log 2>&1
Note that I just put a node_env.txt
file in my app root that sets production mode, because I hate environment variables. You can just do NODE_ENV=production
right there if you prefer.
请注意,我只是在我的应用程序根目录中放置了一个node_env.txt文件来设置生产模式,因为我讨厌环境变量。如果您愿意,可以直接在那里进行NODE_ENV =生产。
#2
3
Just for the record. The Upstart Cookbook recommends the usage of start-stop-daemon
instead of su
or sudo
when your Upstart version does not implement setuid
.
只是为了记录。 Upstart Cookbook建议在Upstart版本未实现setuid时使用start-stop-daemon而不是su或sudo。
But, unless you are still using 10.04 LTS
(Lucid Lynx) which only has Upstart version 0.6.5, you should be using the setuid/setgid
directives.
但是,除非你仍然使用只有Upstart版本0.6.5的10.04 LTS(Lucid Lynx),否则你应该使用setuid / setgid指令。
#3
1
This has been working for me to set node env variables in upstart.
我一直在努力在upstart中设置节点env变量。
#!upstart
start on runlevel [2345]
stop on runlevel [016]
respawn
script
echo $$ > /var/run/app.pid
exec sudo NODE_ENV=production /opt/node/bin/node /opt/myapp/app.js >> /var/log/app.sys.log 2>&1
end script
#4
0
visudo
has a line to define environment variables to be kept.
visudo有一行来定义要保留的环境变量。
sudo visudo
and add your env to:
并将你的env添加到:
Defaults env_keep="YOUR_ENV ..."
and reboot.
并重新启动。
#1
38
From the sudo man page (Ubuntu version of sudo)
来自sudo手册页(Ubuntu版的sudo)
There are two distinct ways to deal with environment variables. By default, the env_reset sudoers option is enabled. This causes commands to be executed with a minimal environment containing TERM, PATH, HOME, SHELL, LOGNAME, USER and USERNAME in addition to variables from the invoking process permitted by the env_check and env_keep sudoers options. There is effectively a whitelist for environment variables.
有两种不同的方法来处理环境变量。默认情况下,启用env_reset sudoers选项。这将导致与含有TERM,PATH,HOME,SHELL,LOGNAME,除了从由env_check和env_keep sudoers的选项允许调用过程变量USER和USERNAME一个最小的环境中执行的命令。环境变量实际上是白名单。
Sudo is resetting the environment. This is a frustrating aspect of using su
and sudo
in upstart or init scripts. Recent versions of upstart support specifying uid/gid without the use of sudo via the setuid/setgid
directives as in the example below. Also note the use of chdir
.
Sudo正在重置环境。在upstart或init脚本中使用su和sudo是一个令人沮丧的方面。最新版本的upstart支持通过setuid / setgid指令指定uid / gid而不使用sudo,如下例所示。还要注意chdir的使用。
start on filesystem and started networking
respawn
chdir /var/www/yourapp
setuid yourapp
setgid yourapp
env NODE_ENV=production
env PATH=/usr/local/bin:/usr/bin:/bin
env CUSTOM=somevalue
exec /usr/local/bin/node app.js | /usr/bin/multilog s1024000 /var/log/yourapp 2>&1
For older versions of upstart, here's what I used to do to work around it.
对于旧版本的新贵,这是我过去常常要解决的问题。
description "start and stop the example.com node.js server"
start on filesystem and started networking
respawn
chdir /path/to/your/code
exec su -c 'PATH=$PWD/node/bin NODE_ENV=$(cat node_env.txt) ./node/bin/node app/server.js' www-data >> tmp/stdout.log 2>&1
Note that I just put a node_env.txt
file in my app root that sets production mode, because I hate environment variables. You can just do NODE_ENV=production
right there if you prefer.
请注意,我只是在我的应用程序根目录中放置了一个node_env.txt文件来设置生产模式,因为我讨厌环境变量。如果您愿意,可以直接在那里进行NODE_ENV =生产。
#2
3
Just for the record. The Upstart Cookbook recommends the usage of start-stop-daemon
instead of su
or sudo
when your Upstart version does not implement setuid
.
只是为了记录。 Upstart Cookbook建议在Upstart版本未实现setuid时使用start-stop-daemon而不是su或sudo。
But, unless you are still using 10.04 LTS
(Lucid Lynx) which only has Upstart version 0.6.5, you should be using the setuid/setgid
directives.
但是,除非你仍然使用只有Upstart版本0.6.5的10.04 LTS(Lucid Lynx),否则你应该使用setuid / setgid指令。
#3
1
This has been working for me to set node env variables in upstart.
我一直在努力在upstart中设置节点env变量。
#!upstart
start on runlevel [2345]
stop on runlevel [016]
respawn
script
echo $$ > /var/run/app.pid
exec sudo NODE_ENV=production /opt/node/bin/node /opt/myapp/app.js >> /var/log/app.sys.log 2>&1
end script
#4
0
visudo
has a line to define environment variables to be kept.
visudo有一行来定义要保留的环境变量。
sudo visudo
and add your env to:
并将你的env添加到:
Defaults env_keep="YOUR_ENV ..."
and reboot.
并重新启动。