I have a systemd unit which runs as a specific user (not root). This unit has an OnFailure with 1 other systemd unit. In this OnFailure unit the ExecStart starts a shell script which sends a email. I want to pass to this shell script the user from the originating system unit which failed. How can I achieve this ?
The usual way to start a unit whilst passing a parameter is to use instances and start
unit@parameter then recover this value inside the unit with
For example, using
--user for testing, file
[Unit] User=otheruser OnFailureemail@example.com [Service] ExecStart=/bin/bash -c 'sleep 3;exit 7'
[Unit] Description=%I Instance of Fail Service [Service] ExecStart=/bin/bash -c 'echo got %i' StandardOutput=journal
Now the commands
systemctl --user daemon-reload systemctl --user start tryfail journalctl -f
should show you the fail service being started with
otheruser as info. Obviously, the
User= is ignored when testing with
--user, but this is of no consequence.