execute arbitrary bash code/variable substitution in systemd units
[Unit]
Description=Demo variable passing inside oneshot service
[Service]
Type=oneshot
ExecStartPre=/bin/bash -c 'systemctl set-environment DATE_%i=`date +%%T`'
ExecStart=/bin/bash -c 'systemctl show-environment'
ExecStop=/bin/bash -c 'systemctl set-environment DATE_%i'
This demo shows 3 extra things:
You can start a templated unit, and the environment variable
allow simple strings, i.e.: systemctl start envvar@myname
would populate DATE_myname
You must escape %
characters inside commands or systemd would substitute with own values.
You can clean the environment variables automatically with ExecStop=
directive.