This questions stems from @FrustratedWithFormsDesigner comment.
How do I make export
environment variables set within an init.d script available to downstream processes started within that same init.d script?
In more detail, I am on an Amazon EC2 instance and using an init.d script to control my node
.js server that is in turn controlled by nodemon
which monitors for file changes and restarts node
as needed. So in short, my init.d script starts nodemon
which then starts node
. Environment variables set with export
within the init.d script seem to be available to nodemon
, but unavailable to node
. How do I make these environment variables available to node
as well?
Currently, only init.d is available in my Amazon EC2 instance so is this even possible with init.d scripts, and if not, is this possible with upstart or preferably systemd?
Best Answer
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:
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.