Environment variable not being picked up by app when started with Upstart (Ubuntu)
Solution 1:
This question seems to contain a lot of sub-questions about how to handle environment variables in an Upstart script, so I thought I would share a few things I do to handle and test my environment for Upstart and Cron.
The first thing to know about Upstart, and the like, is it does not share the same environment as your active terminal, so something like:
$ export FOO=bar
$ start upstart-name
does not work because upstart-name
will not actually see the FOO
you just exported into the current shell because Upstart runs in a clean environment.
You can mimic the clean environment that Upstart (and Cron) runs in with this handy method that I cribbed from another answer many moons ago:
# makes the current shell have an environment like cron or upstart
# http://stackoverflow.com/a/2546509/5006
function cronenv() {
cron_env+="HOME=$HOME\n"
cron_env+="LOGNAME=$LOGNAME\n"
cron_env+="PATH=/usr/bin:/bin\n"
cron_env+="SHELL=/bin/sh\n"
cron_env+="PWD=$PWD\n"
if [[ -n $LC_ALL ]]; then
cron_env+="LC_ALL=$LC_ALL\n"
fi
env - `echo -e $cron_env` /bin/sh
}
Just dump that in your .bash_profile
or the like and you can run commands as Upstart would run them:
$ cronenv
$ some/big/command --to --run --as --upstart
Now, let's actually set up a shared environment on our box, we'll do that by putting our environment variables in /etc/profile.d/environ.sh
# /etc/profile.d/environ.sh
export FOO=bar
and then sourcing that in our Upstart script:
script
# source our common environment file
. /etc/profile.d/environ.sh
exec some/big/command --to --run --as --upstart
end script
Now we'll be able to "see" our environment on any shell and any Upstart script that sources our common file (by default, ssh on Ubuntu will source everything from /etc/profile.d
). Yay!
Solution 2:
Put the env stanza in your upstart job description file.
http://upstart.ubuntu.com/wiki/Stanzas#env
env PIDFILE=/var/run/myprocess.pid
Or whatever. It's all in the documentation.