Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Forked from nickjacob/systemd-prblm.service
Last active January 26, 2018 02:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/0ae0d2686ee5da5f245f330e3a6a3329 to your computer and use it in GitHub Desktop.
Save tzkmx/0ae0d2686ee5da5f245f330e3a6a3329 to your computer and use it in GitHub Desktop.
execute arbitrary bash code/variable substitution in systemd units

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.

[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'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment