Stop letting your systemd timers hide their exit codes from you
Cron & Task Scheduling (Cron/Systemd Timers/At)
Stop letting your systemd timers hide their exit codes from you
🧩 The Challenge
Setting up a systemd timer is great until it fails silently in the background, leaving you to wonder why the database backups didn’t happen for three days. You check the logs, but everything looks like it finished “successfully” even when the script inside clearly crashed.
💡 The Fix
Use the OnFailure directive in your service file to trigger a secondary unit that sends an alert when things go sideways. It saves you from having to write custom error handling in every single bash script you maintain.
[Unit]
Description=My Important Backup
OnFailure=status-email@%n.service
[Service]
ExecStart=/usr/local/bin/backup.sh
Type=oneshot
SUCCESS: [Your command here]
⚙️ Why It Works
Systemd doesn’t automatically treat a non-zero exit status as a reason to notify anyone, so this linkage forces a separate state change you can actually hook into. This setup creates a clean separation between the work being done and the reporting of its health.
🚀 Pro-Tip: Keep your email-on-failure templates generic so you can reuse the same notification service for dozens of different jobs.
Linux Tips & Tricks | © ngelinux.com | 11/11/2026
