How to Monitor Cron Jobs and Get Notified on Failures Automatically
Cron jobs are powerful, but their silent nature makes failure detection tricky. If a job fails silently, it could go unnoticed for days — or longer. In this guide, you’ll learn how to properly monitor cron jobs, log their output, and set up automatic notifications for any issues, so you're never in the dark again. 1. Redirect Output to a Log File By default, cron jobs discard output unless redirected. To keep logs: 0 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1 >> appends stdout to the log 2>&1 redirects stderr to stdout so all output is captured Rotate these logs with logrotate to prevent bloat. 2. Use MAILTO for Email Alerts Add this to the top of your crontab: MAILTO="your@email.com" If your job produces any output (including errors), it will be emailed to you. Make sure your server has mail or an MTA (e.g. Postfix) installed. 3. Only Email on Failure Wrap your job to send mail only when it fails: #!/bin/bash if ! /path/to/script.sh >> /var/log/myjob.log 2>&1; then echo "Cron job failed at $(date)" | mail -s "Cron Failure" your@email.com fi This prevents spam from successful jobs and keeps alerts meaningful. 4. Send Slack or Discord Notifications To send a webhook alert: #!/bin/bash if ! /path/to/script.sh >> /var/log/myjob.log 2>&1; then curl -X POST -H 'Content-type: application/json' \ --data '{"text":"❗ Cron job failed: /path/to/script.sh"}' \ https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX fi Similar webhooks work for Discord, Mattermost, etc. 5. Use Health Check Services Tools like healthchecks.io, Cronitor.io, or Dead Man’s Snitch let you monitor your jobs externally: Ping their URL at the end of a successful run If they don’t hear from your job in time, you get notified #!/bin/bash /path/to/script.sh && curl -fsS https://hc-ping.com/your-uuid This approach is robust and independent of your local mail setup. 6. Use Exit Codes Always ensure your cron scripts return appropriate exit codes. This is essential for all alerting logic: 0 means success Any non-zero value indicates failure Be explicit in your scripts: exit 1 # failure exit 0 # success 7. Centralized Monitoring with Systemd Timers (Optional) If you’re using systemd, consider replacing cron with systemd timers, which include built-in logging, failure tracking, and retry mechanisms. Conclusion Monitoring cron jobs isn't optional if you care about reliability. Whether you use built-in email, external monitoring services, or custom webhook notifications, proactive alerting will save you from invisible outages and broken tasks. If this helped you tighten up your cron strategy, consider supporting my work: buymeacoffee.com/hexshift
Cron jobs are powerful, but their silent nature makes failure detection tricky. If a job fails silently, it could go unnoticed for days — or longer. In this guide, you’ll learn how to properly monitor cron jobs, log their output, and set up automatic notifications for any issues, so you're never in the dark again.
1. Redirect Output to a Log File
By default, cron jobs discard output unless redirected. To keep logs:
0 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1
-
>>
appends stdout to the log -
2>&1
redirects stderr to stdout so all output is captured
Rotate these logs with logrotate
to prevent bloat.
2. Use MAILTO for Email Alerts
Add this to the top of your crontab:
MAILTO="your@email.com"
If your job produces any output (including errors), it will be emailed to you. Make sure your server has mail
or an MTA (e.g. Postfix) installed.
3. Only Email on Failure
Wrap your job to send mail only when it fails:
#!/bin/bash
if ! /path/to/script.sh >> /var/log/myjob.log 2>&1; then
echo "Cron job failed at $(date)" | mail -s "Cron Failure" your@email.com
fi
This prevents spam from successful jobs and keeps alerts meaningful.
4. Send Slack or Discord Notifications
To send a webhook alert:
#!/bin/bash
if ! /path/to/script.sh >> /var/log/myjob.log 2>&1; then
curl -X POST -H 'Content-type: application/json' \
--data '{"text":"❗ Cron job failed: /path/to/script.sh"}' \
https://hooks.slack.com/services/XXXXX/XXXXX/XXXXX
fi
Similar webhooks work for Discord, Mattermost, etc.
5. Use Health Check Services
Tools like healthchecks.io, Cronitor.io, or Dead Man’s Snitch let you monitor your jobs externally:
- Ping their URL at the end of a successful run
- If they don’t hear from your job in time, you get notified
#!/bin/bash
/path/to/script.sh && curl -fsS https://hc-ping.com/your-uuid
This approach is robust and independent of your local mail setup.
6. Use Exit Codes
Always ensure your cron scripts return appropriate exit codes. This is essential for all alerting logic:
-
0
means success - Any non-zero value indicates failure
Be explicit in your scripts:
exit 1 # failure
exit 0 # success
7. Centralized Monitoring with Systemd Timers (Optional)
If you’re using systemd, consider replacing cron with systemd timers
, which include built-in logging, failure tracking, and retry mechanisms.
Conclusion
Monitoring cron jobs isn't optional if you care about reliability. Whether you use built-in email, external monitoring services, or custom webhook notifications, proactive alerting will save you from invisible outages and broken tasks.
If this helped you tighten up your cron strategy, consider supporting my work: buymeacoffee.com/hexshift