1

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 ?

1

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:

[Unit]
User=otheruser
OnFailure=fail@otheruser.service
[Service]
ExecStart=/bin/bash -c 'sleep 3;exit 7'

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

[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.

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.