Making environment variables available for downstream processes started within an init.d script

I recommend switching to a systemd based Linux distro, like Fedora or Ubuntu 16.04. systemd can easily pass environment variables to your process AND it can handle automatically restarting your process it fails as well as starting it at boot. Logging is also nicely handled by systemd`s journald. There's also not the overhead of installing or running anything else, since systemd is part of of the OS distribution.

Here's an example of setting two environment environment variable with systemd in a unit file:

Environment="ONE=one" 'TWO=two two'

Full docs are here.

Your problem was partly one of complexity, as both the init system and the process manager handled passing environment variables. With systemd as both the init system and the process manager, a layer of complexity is removed.

In addition to using Environment directive in the systemd service itself as suggested in this answer, another option is the EnvironmentFile directive.

Please note that I am no expert in this area (obviously, I am the author of this question); I am only summarizing to the best of my ability the Fedora Wiki. Feel free to edit/correct me.

EnvironmentFile directive

Create a file containing new-line-separated variable assignments:

# myservice.env

Then, in your service unit file, use the EnvironmentFile directive:


Within your unit file, use ${ENV1} and $ENV1 to refer to the variables set within myservice.env.

The "-" on the EnvironmentFile= line ensures that no error messages is generated if the environment file does not exist.