[systemd-devel] directive for executing a script on service failure

Alexandre Detiste alexandre.detiste at gmail.com
Fri Feb 6 22:31:14 PST 2015


Le vendredi 6 février 2015, 21:23:14 Mantas Mikulėnas a écrit :
> On Fri, Feb 6, 2015 at 5:26 PM, George Karakougioumtzis <
> mad-proffessor at hotmail.com> wrote:
> 
> > Hi. Congrats for the near perfect job on systemd! I was searching for a
> > directive to execute a script upon systemd service failure. I would like
> > to receive desktop notifications about such  failures. I stumbled upon
> > OnFailure and FailureAction but these have hardcoded list of actions?
> >
> 
> One of those actions is "start an arbitrary unit", which could handle
> notifications... Unfortunately it doesn't actually pass any failure
> information to that unit. 
Hi,

OnFailure= can be an instanciated unit, like "OnFailure=failure@%i.service"
then your failure at .service include this line

[Service]
Type=oneshot
ExecStart=/usr/local/bin/user_notification %i

then your notification script get failed service name as argv[1]

> Maybe others will have better suggestions.

for user notification I use this, I guess it's not optimal:

#!/bin/bash
user=$(loginctl list-sessions | grep seat0 | awk '{print $3}')
if [ -n "$user" ]
then
	export DISPLAY=:0
	su $user -c "notify-send '$1 FAILED' -i /usr/share/icons/oxygen/48x48/status/dialog-warning.png  "
else
	echo "$1 FAILED " | wall
fi


More information about the systemd-devel mailing list