Systemd Environment and EnvironmentFile not working

centos7systemd

I've built an application and a systemd unit for it. The systemd unit works fine, but as the dev and prod environments have diverged I've started moving config out to environment variables and I can't seem to get them to work in systemd.

I've tried system-wide environment variables, and they're visible to the OS, but not the program, so i started looking at building them into systemd units.

First I tried using EnvironmentFile

I created an environment file that simply had

LCSQLH=localhost
LCSQLU=application

in it as /etc/lc.sh and

[Unit]
Description=Service for this app

[Service]
EnvironmentFile=/etc/lc.sh
ExecStart=/usr/bin/env python /opt/app/__init__.py

did a systemctl --system daemon-reload but no, my app errored:

Jan 27 14:24:59 machine.host env[630]: KeyError: 'LCSQLU'

I saw some had:

EnvironmentFile=-/etc/lc.sh

I tried that…. nope…

So I tried putting them in individually

[Service]
Environment="LCSQLH=localhost"
Environment="LCSQLU=application"
ExecStart=/usr/bin/env python /opt/app/__init__.py

Yet again, no…

So I heard about the idea of /etc/systemd/service_name.service.d so I put a service.conf in there with the environments in (same format as above) but no…

My application has no access to these environment variables.

If I export them (either manually in my shell or using /etc/profile.d/) and run my app directly it works, so it is that these aren't being set rather than an app issue.

This is Centos 7.3 and I've chosen environment variables rather than a hardcoded config because it may run on either linux or windows, so don't want to bury a config file in /etc/

Best Answer

I ran into the same trouble on RHEL 7.3 and found this:

You may then refer to variables set in the /etc/sysconfig/httpd file with ${FOOBAR} and $FOOBAR, in the ExecStart lines (and related lines).

This makes me think that the purpose for Environment and EnvironmentFile is not at all what you and I expected (setting environment variables for the process started by the systemd unit), but only applies for the immediate expansion of the ExecStart line.

Perhaps I'm completely off-base and this is a bug in systemd (I hope so). But I stopped going down the path you are trying and did it another way: In my case I needed to set LD_LIBRARY_PATH, so I did that via creating /etc/ld.so.conf.d/new_file.conf and then running ldconfig. I also attempted to use system-wide variables in /etc/profile.d/new_file.sh, but apparently setting just LD_LIBRARY_PATH was enough for this service (mariadb) and so I don't actually know if the variables I was setting in /etc/profile.d/new_file.sh were functioning.