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:
does not work because
upstart-name
will not actually see theFOO
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:
Just dump that in your
.bash_profile
or the like and you can run commands as Upstart would run them: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
and then sourcing that in our Upstart 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!