tzkmx
10/18/2017 - 2:39 PM

execute arbitrary bash code/variable substitution in systemd units

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:

  1. You can start a templated unit, and the environment variable allow simple strings, i.e.: systemctl start envvar@myname would populate DATE_myname

  2. You must escape % characters inside commands or systemd would substitute with own values.

  3. You can clean the environment variables automatically with ExecStop= directive.