Ubuntu – Environment variable not being picked up by app when started with Upstart (Ubuntu)

environment-variablesUbuntuupstart

How does one go about getting a new environment variable picked up system-wide without restarting the server?

As its an Ubuntu 11.x server, I'm using Upstart to start/restart processes.

I added a new environment variable to /etc/environment that needs to be picked up by one of my app's processes.

If I start the process directly from my shell, the variable is being picked up and all is fine (I've already sourced /etc/environment for my session). However, when I start/restart the process using its Upstart service name, the variable is not being seen.

 sudo start app-name

I'm pretty sure that if I restarted the server the new variable will get picked up, however I would like to try to avoid that. I have some other live processes running on it that I'd like to avoid interrupting if at all possible.

Best Answer

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!

Related Topic