Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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

I need to export a variable FOO=stringhere so that system processes could use it. And the place I would like to do this, is Upstart job.

Unfortunately, I tried as per Upstart manual to do env FOO=stringhere http://upstart.ubuntu.com/cookbook/#environment-variables

But after the system is booted, echo $FOO returns nothing.

Am I trying to do something impossible, or are there any gotchas I should know?

share|improve this question
up vote 6 down vote accepted

As the page you refer to says,

Upstart allows you to set environment variables which will be accessible to the jobs whose job configuration files they are defined in.

The indicates that the variables are set in the environment used by the system daemons at startup. The user environment is configured separately once you log in.

If you want to check whether the daemon knows about the variable you set, you can test that by temporarily modifying its startup script and adding something like logger "=============== The variable FOO is set to $FOO =====================". You should then see the information logged during boot-up (look in /var/log/messages for example).

share|improve this answer
Any ideas on how I could work around this feature? – Coder Sep 15 '11 at 14:41
What do you mean by feature here? What is your goal - knowing if the variable is used at system startup (see my edit for that) or having the variable exported to all your users' shell sessions? – rozcietrzewiacz Sep 15 '11 at 14:46
Yes, so that every user on the system could type echo $FOO and get the stringhere. – Coder Sep 15 '11 at 14:50
You answered "yes" to which one of the two @rozcietrzewiacz's question? – andcoz Sep 15 '11 at 14:58
If you really just want to have a "public" variable visible to all users, then a system startup job is not the place to set it. But your question points at a problem of setting a variable which, as you wrote, system processes could use. This is a different issue (also useful). So please decide to either modify the question or ask a separate one. – rozcietrzewiacz Sep 15 '11 at 16:36

In your example that variable could be used in the same file.

Note from upstart cookbook.

Note that a Job Configuration File does not have access to a user's environment variables, not even the superuser. This is not possible since all job processes created are children of init which does not have a user's environment.

Further explanation from upstart cookbook.

Lets say you write env TESTING=123 in your *.conf file, then you can use this variable in your script block below (the one you used)

  # prints "TESTING='123'" to system log
  logger -t $0 "TESTING='$TESTING'"
end script

Or a bit more sophisticated approach would let you use you variable in jobs that 'listen' to your job:

# /etc/init/A.conf
start on wibble
export foo

# /etc/init/B.conf
start on A
  logger "value of foo is '$foo'"
end script
share|improve this answer

What you want to accomplish is better suited to /etc/profile which is source'ed by login shells.

export foo="String here"

share|improve this answer

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.