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.


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() {

  if [[ -n $LC_ALL ]]; then

  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:

  # 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!


Put the env stanza in your upstart job description file.


env PIDFILE=/var/run/myprocess.pid

Or whatever. It's all in the documentation.

  • Thanks, I can totally make that work. I'd like to go further and make it global and permanent though. I'm setting a variable that multiple processes can use to determine if they're in a development or production environment. The conf file will work for now because I develop on a Mac, but I eventually will have an integration environment running Ubuntu and each server will run multiple separate app processes. It would be convenient for them all not to rely on the confs.
    – Geuis
    Dec 17 '11 at 11:09
  • What? You wanna use Upstart. Good. You need to specify things in the Upstart's configuration so that it knows how to start the processes. It's either that, or you make a wrapper script that sets and exports environment variables (messy) Or modify the default bash profile, so they're always set and exported (Insanely Messy). Dec 17 '11 at 11:12
  • The right way to do this is to use something like Puppet to manage your different environments, and then Puppet can manage the different configuration files, across many servers, without fuss. Dec 17 '11 at 11:13
  • Tom, I'm running into an odd problem. Using env, I can't set any custom environment variables. If I set system defined vars, they are being read by the app. But my own aren't. For example, I set without quotes in the conf, "env DISPLAY=foo" and DISPLAY is picked up with a value of foo. But if I set "env TESTENV=foo" the variable TESTENV isn't even detected.
    – Geuis
    Dec 17 '11 at 11:53
  • 1
    I'll give rc.local a try after a bit of sleep. Thanks for the setgid tips. I've also been hesitant about sudo but that's what most of the examples I've seen do and it works. For my existing apps, its been working pretty reliably for a while now but I'm going to look into your link and see how it could be done more securely. Really appreciate your help tonight and hopefully I can figure out the rest.
    – Geuis
    Dec 17 '11 at 13:50

I had the same problem. My startup script worked fine from the command line, but not from upstart script.

I simply had to source my . /home/username/.bashrc in my upstart script to get it working with all the correct environment variables.


  cd /home/me
  . /home/me/.bashrc

end script
  • This does not work for me (Ubuntu Server 14.04.1). I get no error when sourcing /home/<user>/.bashrc inside the Upstart script, but the env vars don't stick around for the next command (the app/service being started).
    – odigity
    Jan 12 '15 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.