Dynamic variables in systemd service unit files
If you are careful you can incorporate a small bash script sequence as your exec command in the instance service file. Eg
ExecStart=/bin/bash -c 'v=%i; USE_GPU=$${v%:*} exec /bin/mycommand'
The $$
in the string will become a single $
in the result passed to bash, but more importantly will stop ${...}
from being interpolated by systemd. (Earlier versions of systemd did not document the use of $$
, so I don't know if it was supported then).
No built in way. You need to do these things before your service starts. One way would be putting it to an environment file.
[Service]
# Note you need to escape percentage sign
ExecStartPre=/bin/sh -c "my_awesome_parser %%i > /run/gpu_service_%i"
EnvironmentFile=/run/gpu_service_%i
ExecStart=...
It looks like you can indeed set environment variables inside a systemd unit file...
Per suggestions from commenters, here is the solution:
Using environment variables in systemd units
Environment directive
systemd has an Environment directive which sets environment variables for executed processes. It takes a space-separated list of variable assignments. This option may be specified more than once in which case all listed variables will be set. If the same variable is set twice, the later setting will override the earlier setting. If the empty string is assigned to this option, the list of environment variables is reset, all prior assignments have no effect. Environments directives are used in built-in Container Linux systemd units, for example in etcd2 and flannel.
With the example below, you can configure your etcd2 daemon to use encryption. Just create
/etc/systemd/system/etcd2.service.d/30-certificates.conf
drop-in for etcd2.service:[Service] # Client Env Vars Environment=ETCD_CA_FILE=/path/to/CA.pem Environment=ETCD_CERT_FILE=/path/to/server.crt Environment=ETCD_KEY_FILE=/path/to/server.key # Peer Env Vars Environment=ETCD_PEER_CA_FILE=/path/to/CA.pem Environment=ETCD_PEER_CERT_FILE=/path/to/peers.crt Environment=ETCD_PEER_KEY_FILE=/path/to/peers.key
Then run
sudo systemctl daemon-reload
andsudo systemctl restart etcd2.service
to apply new environments to etcd2 daemon.
Quoted text taken from the following URL: https://coreos.com/os/docs/latest/using-environment-variables-in-systemd-units.html