Score:5

Why is my Systemd service not passing parameters from my EnvironmentFile?

sa flag

I'm new-ish to systemd. I've got a service that's been working fine with the ExecStart command line hard-coded. Now I'm trying to parameterize it, and I found EnvironmentFile written up online. So I modified my service file as follows (some things obfuscated for privacy):

[Service]
Type=simple
EnvironmentFile=/etc/my-service/my-service-systemd.cfg
ExecStart=/usr/local/bin/my-service -port=$PORT -log-level=$LOG_LEVEL -creds=$CREDENTIALS_FILE
ExecStop=/bin/kill -15 $MAINPID

I checked, and the environment file /etc/my-service/my-service-systemd.cfg is definitely there:

CREDENTIALS_FILE=/etc/my-service/my-service.pwd
LOG_LEVEL=2
PORT=443

But when I run the service, it fails, and the logs indicate that all parameters are coming in empty, i.e. the service file is not passing in values from the EnvironmentFile.

What am I doing wrong?

Score:7
in flag

There are rules on the expansion of the $ prefix in ExecStart and so on. There is a difference between $var and ${var}. The former will only be interpolated if it occurs as a word on its own (surrounded by whitespace). The latter does not have this restriction. So use, for example,

-port=${PORT}

The reference for this is under the heading COMMAND LINES in man systemd.service.

Score:1
ru flag

It may be related to file permissions. See if this fixes it:

chown root:root /etc/my-service/my-service-systemd.cfg 
chmod 0640 /etc/my-service/my-service-systemd.cfg
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.