Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
add comment

3 Answers

up vote 4 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
    
Well, you could force the startup script append a specific variable to the system profile file, but are you sure it is what you want? Don't you just need to have the variable visible to all users (but not necessarily being exported by a specific startup job)? –  rozcietrzewiacz Sep 15 '11 at 15:02
show 2 more comments

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)

script
  # 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
script
  logger "value of foo is '$foo'"
end script
share|improve this answer
add comment

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
add comment

Your Answer

 
discard

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.