Catch your cron jobs failing before your boss does
Cron & Task Scheduling (Cron/Systemd Timers/At)
Catch your cron jobs failing before your boss does
🧩 The Challenge
Setting up a cron job is easy, but finding out it’s been silently failing for three weeks while you were on vacation is a nightmare. I’ve wasted way too much time grepping through huge log files just to find out a simple path issue killed my backup script.
💡 The Fix
Pipe the output directly to a system notification or a quick mail command so you actually know when things hit the fan. If the command fails, you’ll get an alert instantly instead of guessing.
* * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1 || echo "Script failed on $(hostname)" | mail -s "Cron Job Failed" admin@example.com
⚙️ Why It Works
By capturing both standard output and standard error into a log file, you keep a history of what happened, while the OR operator ensures you only get that email notification when the exit code is non-zero. It’s the difference between a proactive fix and a panic-induced scramble.
🚀 Pro-Tip: Toss an ‘exit 1’ at the top of your script if a prerequisite folder is missing to force that mail alert to fire early.
Linux Tips & Tricks | © ngelinux.com | 11/24/2026
