Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What is the best way to provide the environment variables defined in /etc/environment to an upstart service?

I think simply sourcing them with . in a script section does not work, because the scripts are executed by sh which would need an additional export in front of every definition...

share|improve this question
up vote 13 down vote accepted

I finally got an answer on the #upstart IRC channel. At some point, upstart will get proper PAM support and thus read /etc/environment itself. Until then, the trick is to execute the command with su. su uses PAM and will set up the proper environment. Example:

    exec su root -c /usr/sbin/job_needing_envs
end script
share|improve this answer
Thank you for posting the answer, you've helped me in exactly the same issue. – Maxim Veksler Dec 20 '10 at 9:44

I tend to use eval $(cat /etc/environment | sed 's/^/export /')

It takes each line in /etc/environment, prepends export, and evaluates it:

exec /bin/bash <<'EOT'
  eval $(cat /etc/environment | sed 's/^/export /')
end script
share|improve this answer
Why do you use exec and not just put the export directly in the script block? – Guss Oct 15 '15 at 14:58

Add this to your script:

. /etc/environment
export VAR1 VAR2 VAR3

where the variables you need are specified in place of the "VAR1" style placeholders.

share|improve this answer
That way I have to manually keep the upstart configuration and /etc/environment in sync, which is (in my opinion) not any better than defining the variables twice... – Nikratio Apr 2 '10 at 16:17
I wouldn't use /etc/environment to define all your needed variables. Leave that as a static file. On my system, I could only find a few scripts that use it anyway. Create a file called something like /etc/environment.local and put your variables and exports in there and source that file. Then you only have to maintain that one file. – Dennis Williamson Apr 2 '10 at 17:03
/etc/environment is read by (and not by any scripts), so it is available for any login. Only programs started by upstart don't have access to that file by defaulht, unfortunately. – Nikratio Apr 5 '10 at 13:23

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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