1

we've started running our EC2 apps as services by creating an /etc/init.d/[appname] script and executing it using something like:

service [appname] start|stop|restart

When doing this, however, our apps don't have access to environment variables. How can a process started as a service access environment vars? Or, conversely, how can we set environment vars so that they can be accessed by processes running as services? Specifically, we rely on these vars to know what environment we are running in... which is good to know...

David Erwin
  • 1,270
  • 2
  • 11
  • 14
  • They certainly do have access to *their own* environment variables. *Whose* environment you want to access? ("the one for logged-in users" can be made up of variables defined in a number of places) – Anton Kovalenko Feb 14 '13 at 21:42
  • Which services, which environment variables are you really thinking of? – Basile Starynkevitch Feb 14 '13 at 23:17
  • It turns out that my problem was proprietary. The variables were being loaded into my env through a script set up by our administrators that I was not aware of. Running my app as a service didn't have access to them, so simple sourcing /etc/envscript worked. – David Erwin Feb 19 '13 at 15:49

1 Answers1

2

A program can access its environment variables using the environ variable or more usually the getenv(3) library function.

You can query the environment of some process 1234 using /proc file system, notably thru /proc/1234/environ (be aware that the entries are null terminated, so read the man page).

The starting scripts in /etc/init.d/ are often shell scripts so you can export FOO=var to set the environment variable FOO to var inside these scripts. (you need to add that export line inside the script itself, or to source a file doing that from that init script).

See also this question.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547