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 %I. For example, using --user for testing, file ~/.config/systemd/user/tryfail.service:

ExecStart=/bin/bash -c 'sleep 3;exit 7'

and ~/.config/systemd/user/fail@.service

Description=%I Instance of Fail Service
ExecStart=/bin/bash -c 'echo got %i'

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.