How to Monitor a Cron with MoniTao
Step-by-step guide to never miss a silently failing cron again.
Cron tasks are essential but often forgotten. This guide shows you how to monitor them effectively.
In just a few minutes, configure a heartbeat that will alert you if your cron doesn't run.
Why Monitor Your Crons?
Crons can fail silently for many reasons:
- The script has an unhandled error
- The server was restarted without reactivating crons
- A permission change blocks execution
- The database or a dependent service is unavailable
Step-by-Step Configuration
Follow these steps to configure monitoring:
Step 1: Create a Heartbeat Job
In MoniTao, create a new heartbeat job with the interval matching your cron. For example, 24 hours for a daily cron.
Step 2: Modify Your Crontab
Add a curl call at the end of your cron command. Use && so the ping is only sent on success.
# Crontab original
0 2 * * * /usr/local/bin/backup.sh
# Crontab avec heartbeat MoniTao
0 2 * * * /usr/local/bin/backup.sh && curl -s -X POST \
https://monitao.com/api/heartbeat/ping/VOTRE_TOKEN \
-H "Authorization: Bearer VOTRE_SECRET"
Step 3: Handle Errors
For better handling, create a wrapper script that signals start, success, or failure:
#!/bin/bash
# backup.sh avec gestion d'erreur
TOKEN="VOTRE_TOKEN"
SECRET="VOTRE_SECRET"
URL="https://monitao.com/api/heartbeat"
# Signal debut
curl -s -X POST "$URL/start/$TOKEN" -H "Authorization: Bearer $SECRET"
# Execution du backup
/usr/bin/mysqldump -u user -p'password' database > /backup/db.sql
RESULT=$?
# Signal succes ou echec
if [ $RESULT -eq 0 ]; then
curl -s -X POST "$URL/ping/$TOKEN" -H "Authorization: Bearer $SECRET"
else
curl -s -X POST "$URL/fail/$TOKEN" -H "Authorization: Bearer $SECRET"
exit 1
fi
Configure Interval and Grace Period
Adapt the configuration to your cron:
- Interval - Must match the cron frequency (1h, 24h, etc.)
- Grace Period - Additional wait time before alert (15-60 min recommended)
- Alerts - Email by default, SMS available with Business
Concrete Examples
- Daily database backup cron at 2am
- Hourly temp file cleanup cron
- Third-party API sync cron every 15 minutes
Summary
- Create a heartbeat job with the right interval
- Add the curl to your cron or create a wrapper script
- Test and verify pings are arriving
Frequently Asked Questions
My cron sometimes takes longer, how to avoid false alerts?
Increase the grace period to give the cron time to complete.
Can I monitor a cron running on multiple servers?
Yes, create a heartbeat job per server or one if only one should run.
Does the heartbeat add latency to my cron?
The HTTP ping takes a few milliseconds, completely negligible.
Can I see the execution history?
Yes, each ping is recorded with timestamp and duration in the dashboard.
Useful Links
Ready to Sleep Soundly?
Start free, no credit card required.