0

I have the following systemd .target:

[Unit]
Description=My Target
After=network-online.target
Wants=one-service@%i.service
Wants=two-service@%i.service
BindsTo=one-service@%i.service
BindsTo=two-service@%i.service

[Install]
WantedBy=default.target
Also=one-service@%i.service
Also=two-service@%i.service

And the (one, though both are the same, just different ExecStart= values) .service:

[Unit]
Description=one service
PartOf=my@%i.target
ReloadPropagatedFrom=my@%i.target

[Service]
Type=forking
ExecStart=/path/to/my-first-command -${SOME_VAR_DEFINED_IN_ENV_FILE}
PIDFile=/path/to/pidfile
SuccessExitStatus=0
TimeoutStartSec=180s
TimeoutStopSec=180s
RemainAfterExit=true

[Install]
WantedBy=my.target

I then enable the target with systemctl enable [email protected]

I then execute systemctl edit [email protected]; with the following:

[Service]
User=oneusername
Group=onegroup
EnvironmentFile=/home/%u/etc/%i.environment

I then execute systemd daemon-reload, followed by systemctl start [email protected], which fails because the variables in the EnvironmentFile aren't loaded.

However, when I enable and start the target, the variables I've placed in the environment file do not appear to be read, as the service fails because it can't find the variable substitution. I know I'm doing something wrong, but I don't know what.

Tried to enable systemd target with service, service has variables that should be read by EnvironmentFile, but the EnvironmentFile isn't being read.

EDIT: corrected typo, originally there was Service] when it should have been [Service]

0

1 Answer 1

2

The problem here is not with your EnvironmentFile, but that using variables in the path of the executable for Exec* is simply not supported. There's an open RFE, but I think it unlikely it would be implemented. You could work around that by using /bin/sh -c '...' to make the shell do the expansion instead.

8
  • Ok, so - if I specify an EnvironmentFile directive (or Environment directive) as an array - let's say myArray=("val1" val2"), and then run /bin/bash -c 'for entry in ${myArray[*]}; do echo ${entry}; done' would I be able to reference that array variable if defined by Environment or within EnvironmentFile? Commented Apr 24 at 13:29
  • No, systemd doesn't support arrays either. Commented Apr 24 at 14:35
  • Understood, but if the EnvironmentFile is sourced, bash should be able to handle it, no? Alternately, is there a way to tell bash via /bin/bash -c 'source ${environmentfile} ...' ? Commented Apr 24 at 14:58
  • also, based on your initial response, the process identified by this link (flatcar.org/docs/latest/setup/systemd/environment-variables) shouldnt work - and that seems to be the experience im getting Commented Apr 24 at 14:59
  • I don't see anything particularly unusual in that link. It's using a simple path for the executable (/usr/bin/etcd2) unlike your example in the question (/path/to/my-first-command-${SOME_VAR_DEFINED_IN_ENV_FILE}) Commented Apr 24 at 15:04

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.