17

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...

Falcon Momot
  • 25,584
Nikratio
  • 645
  • 5
  • 13

3 Answers3

16

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:

script 
    exec su root -c /usr/sbin/job_needing_envs
end script
chicks
  • 3,915
  • 10
  • 29
  • 37
Nikratio
  • 645
  • 5
  • 13
3

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

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

script
exec /bin/bash <<'EOT'
  eval $(cat /etc/environment | sed 's/^/export /')
  do_what_you_need_to
EOT
end script
Jrgns
  • 161
1

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.