4

I have a set of upstart scripts, generated by foreman from a Procfile, to start my web service and async task queue worker. The way foreman generates the scripts is as follows:

  • app
  • app-web
  • app-web-1
  • app-worker
  • app-worker-1

where "app-web" and "app-worker" are set to start on starting "app", and "app-web-1" and "app-worker-1" are set to start on starting "app-web" and "app-worker" respectively. As I understand it, this structure is to allow you to easily add further web and worker processes when load increases, while still managing the whole thing from the top-level "app" job.

So, I want to set various environment variables in my jobs. I can of course put them directly into the bottom-level scripts, but it would be better to put them in the top level so they only have to be defined once. I thought I would be able to do this in app.conf by simply using env and export:

env FOO=bar
export FOO

but this doesn't seem to work: it's fine if I put it in the middle "app-web" job, but if I put in the top-level "app" it doesn't get inherited by the bottom-level "app-web-1". So, my second attempt was to put that in the top-level script, and then try and re-export in the middle one with just export FOO, but this doesn't work either.

Is it possible to do what I want, or should I simply settle for defining the variables in multiple places?

4

You can do this in two way: export variable in the middle script or save it in file and source it in lowest script.

I tested this example on lubuntu 12.04:

myapp.conf is the main script and export MYAPP_FOO variable:

# myapp - 
description "export environment variable"
env MYAPP_FOO=bar
export MYAPP_FOO

task
console log
script
    exec  echo "$MYAPP_FOO - myapp" 
end script

app-web.conf is the middle script that export parent's variable and define and export MYAPP_FOO1

# app-web - 
description "export environment variable"
start on starting myapp
env MYAPP_FOO1=bar1
export MYAPP_FOO1
export MYAPP_FOO

task
console log
script  
   exec  echo "$MYAPP_FOO - app-web - $MYAPP_FOO1" 
end script

Finally app-web1.conf print variable:

# app-web1 - 
description "export environment variable"
start on starting app-web

task
console log
script  
   exec  echo "$MYAPP_FOO - app-web1 - $MYAPP_FOO1" 
end script

I used sudo start myapp and the result in /var/log/upstart/app-web1.log is:

bar - app-web1 - bar1

Another approach is to define your variable in file and source it where you need:

# app-web1 - 
description "export environment variable"
start on starting app-web

task
console log
script  
   . /path/where/you/want/environmentFile
   exec  echo "$MYAPP_FOO - app-web1 - $MYAPP_FOO1" 
end script

Or you can create environmentFile in the middle script:

# app-web - 
description "export environment variable"
start on starting myapp
env MYAPP_FOO1=bar1

task
console log
script 
   env | grep MYAPP_ > /path/where/you/want/environmentFile 
   exec  echo "$MYAPP_FOO - app-web - $MYAPP_FOO1" 
end script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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