How to monitor a cron job with curl
Cron fails silently: a typo, a full disk, a changed path, a server that was rebooted and lost its crontab. Nothing tells you until you need the output. A heartbeat ping fixes that.
The one-liner
# m h dom mon dow command
*/15 * * * * /opt/scripts/sync.sh && curl -fsS -m 10 --retry 5 -o /dev/null https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID
&& means the ping only fires if the script exits 0. Flags: -f fail on HTTP errors, -sS quiet but show errors, -m 10 give up after 10 s, --retry 5 retry transient network failures, -o /dev/null discard the body.
Pick the right period and grace
For a job that runs every 15 minutes set period 15m and a grace of 5 to 10 minutes. For a nightly job, 1d and 1h. If you prefer to mirror the crontab exactly, choose Cron expression and paste */15 * * * * with your timezone.
Report failures immediately
*/15 * * * * /opt/scripts/sync.sh; curl -fsS -m 10 --retry 5 -o /dev/null https://heartbeat-monitor.morristech.ca/ping/YOUR-CHECK-ID/$?
Here $? is the exit code of the previous command. Exit code 0 is a success ping; any other number is treated as a failure and alerts you right away. See start, fail and run-time pings.
Troubleshooting
- Check
which curlin the cron environment; cron has a minimal PATH. Use/usr/bin/curl. - Run the command by hand first and look at the response (
OK). - Remember cron uses the server's timezone; set the same timezone on cron-expression checks.