I want a message the moment a service fails and not when I next look at the logs.
# myapp.service, [Unit]
OnFailure=notify@%n.service
# [email protected], [Service]
ExecStart=/usr/local/bin/notify.sh "%i failed"
OnFailure= starts the listed unit when the service enters the failed state. The template name [email protected] receives the failed unit’s name as the instance: %n expands to the full name of the failing unit, and %i is the instance in the template. Put a curl call to ntfy or a mail command in notify.sh. It does not fire for a clean stop. If Restart= is set, it fires only after the start limit is reached.
Further reading: systemd.unit manual page