Monitoring cron jobs is essential to ensure scheduled tasks are completed successfully. When a cron job fails, quick notification is critical for timely troubleshooting. Here are several reliable methods to set up alerts for failed cron jobs:
1. Use Cron’s Built-In Mail Alert
By default, cron can email the output of a job to the user who owns the crontab.
Steps:
-
Set the
MAILTOvariable at the top of your crontab: -
Ensure your system has an MTA (Mail Transfer Agent) like
sendmailorpostfixinstalled and configured.
If a job produces any output (stdout or stderr), it will be emailed to the specified address.
2. Redirect Output to Email Only on Failure
Send only errors:
This command:
-
Redirects standard output to
/dev/null -
Keeps standard error, which is piped to
mailif there’s any error
3. Log and Monitor Exit Codes
Create a wrapper script to check the exit code and send alerts:
Then in crontab:
4. Use logger to Log to Syslog
Log job failure with logger and monitor logs with tools like Logwatch, Logrotate, or Splunk.
5. Integrate with Monitoring Tools
Examples:
-
Nagios/Icinga: Set up a service check for the job.
-
Zabbix: Use an external script with trap-based monitoring.
-
Datadog: Monitor cron output logs and create alerting rules.
-
Prometheus + Alertmanager: Export metrics from scripts (e.g., via exit code logs) and trigger alerts.
6. Slack, Discord, or Webhook Notifications
Integrate alert scripts with APIs to send alerts to chat or incident management platforms.
Example using curl and Slack:
7. Log to a Custom File with Timestamp
Maintain your own error log for analysis or alert processing.
Then monitor /var/log/cron_error.log with any log-watching tool.
8. Use at for Retry on Failure
For non-critical, retryable jobs:
This requeues the job via at if it fails.
9. Custom Monitoring with cronwrap or cronitor
-
cronwrap: A shell wrapper to log execution and status.
-
cronitor.io or healthchecks.io: External services to monitor cron schedules and alert on missed or failed runs.
Example using healthchecks.io:
Trigger this at the start or end of your script. If no ping is received, healthchecks.io sends an alert.
Best Practices Summary:
| Method | Pros | Cons |
|---|---|---|
Mail Alert (MAILTO) | Built-in, simple | Needs MTA, noisy |
| Wrapper Script Alert | Flexible, better control | Requires script customization |
| External Monitoring Tools | Scalable, dashboard support | May need subscription |
| Slack/Webhook Alert | Fast, team visibility | API setup required |
| Error Log + Logwatch | Archivable, audit-friendly | Needs separate monitoring |
Combining local logging, error codes, and external notifications provides robust and timely alerts for cron job failures, minimizing downtime and operational issues.