How do you alert when a timer-driven service fails?
IntermediateConceptcron and systemd timers
Answer
Use OnFailure= to start a notification unit, passing the failed unit's name with a template:
# backup.service
[Unit]
OnFailure=notify-failure@%n.service
# [email protected]
[Service]
Type=oneshot
ExecStart=/opt/opskit/bin/opskit-alert -s critical -t "unit %i failed" "check: journalctl -u %i"
Also monitor from outside: a "dead man's switch" (the job pings a heartbeat URL on success, and the monitoring system alerts when pings stop) catches the case where the timer was disabled or the host is down.
More on cron and systemd timers
Explain the five cron time fields with examples.BeginnerWhat is different about the environment a cron job runs in?BeginnerWhat is the difference between a user crontab, `/etc/crontab`, `/etc/cron.d/` and `/etc/cron.daily/`?IntermediateHow do you prevent overlapping cron runs and capture output properly?IntermediateWhat is a systemd timer and how does it compare to cron?IntermediateWrite a minimal systemd service and timer that run a backup at 02:30 daily.Intermediate