How-to guide
How to monitor cron jobs
Cron jobs fail silently. A heartbeat check that expects a ping after every successful run is the simplest way to know the moment one is missed.
3 min read11 sectionsUpdated
On this page
Why cron needs monitoring
Cron runs a command and forgets about it. If the server is replaced and the crontab is not, if a script starts failing after a dependency update, or if the job hangs forever on a network call, nothing tells you. Backups, invoice runs, data syncs, cache warmers and cleanup jobs all fail this way, and the first sign is usually a customer or an auditor.
Heartbeat monitoring fixes this: the job pings a unique URL when it succeeds, and the monitor alerts if the ping does not arrive on schedule.
# m h dom mon dow command
# Nightly backup at 02:00; ping only if the script exits 0
0 2 * * * /opt/jobs/backup.sh >> /var/log/jobs/backup.log 2>&1 && curl -fsS -m 10 --retry 3 "$HB_BACKUP" > /dev/null
# Every 15 minutes: sync orders
*/15 * * * * /opt/jobs/sync-orders.sh >> /var/log/jobs/sync.log 2>&1 && curl -fsS -m 10 --retry 3 "$HB_SYNC" > /dev/nullA reusable wrapper
A small wrapper keeps crontabs tidy, enforces a timeout and records how long the job took:
#!/usr/bin/env bash
# usage: run-job.sh <heartbeat-url> <timeout-seconds> <command...>
set -uo pipefail
url="$1"; limit="$2"; shift 2
start=$(date +%s)
timeout "$limit" "$@"
status=$?
echo "job=$* status=$status duration_s=$(( $(date +%s) - start ))"
[ "$status" -eq 0 ] && curl -fsS -m 10 --retry 3 "$url" > /dev/null
exit "$status"0 2 * * * /opt/jobs/run-job.sh "$HB_BACKUP" 3600 /opt/jobs/backup.sh >> /var/log/jobs/backup.log 2>&1Kubernetes CronJobs
The same idea works in a CronJob: run the task, then ping. Keep the URL in a Secret.
apiVersion: batch/v1
kind: CronJob
metadata: { name: nightly-report }
spec:
schedule: "0 3 * * *"
concurrencyPolicy: Forbid
jobTemplate:
spec:
backoffLimit: 2
activeDeadlineSeconds: 3600
template:
spec:
restartPolicy: Never
containers:
- name: report
image: registry.example.com/report:1.4.0
command: ["sh", "-c", "node build-report.js && wget -q -T 10 -O /dev/null \"$HEARTBEAT_URL\""]
env:
- name: HEARTBEAT_URL
valueFrom: { secretKeyRef: { name: heartbeats, key: nightly-report } }Choosing schedules and grace periods
Set the expected interval to the cron schedule and the grace period to the job's normal duration plus a margin. A daily backup that takes 20 minutes might have a 1-hour grace period; an every-15-minutes sync that takes 2 minutes might have 10 minutes. If a job's duration varies a lot, look at why before widening the grace period.
In VitalRix
Create a heartbeat monitor per job. Each gets a unique ping URL; if the job does not check in within its deadline, the monitor fires and alerts through the same channels and escalation policies as your other monitors. Pair it with log management to search job output by job name. See synthetic monitoring.
What if a job runs but does the wrong thing?
Add a check inside the job, such as "at least one row exported", and exit non-zero when it fails. Then the ping is skipped and you are alerted.
Should I also ping when the job starts?
Optional. A start ping lets you measure duration and detect jobs that hang. Always keep the success ping as the one that clears the deadline.
How do I monitor Windows scheduled tasks?
The same way: add a final step that calls the heartbeat URL with Invoke-WebRequest only when the task succeeded.
Check it now, free
- Cron expression explainer: Cron in plain English, with the next run times.
Related reading
- A Cronitor alternative: VitalRix vs Cronitor: VitalRix vs Cronitor: cron job and heartbeat monitoring, uptime checks, status pages and alerts compared, plus APM and on-call, and when each fits better.
- How to monitor Render: How to monitor apps on Render: web service uptime and health checks, background workers, cron jobs with heartbeats, managed Postgres and Redis, and alerts.
- ECS and Fargate monitoring: Monitor Amazon ECS and Fargate: CPU and memory per service, stopped-task reasons, ALB target health, logs and traces.
- What is alert flapping?: Alert flapping explained: why alerts fire and resolve over and over, and the fixes that work: sustained conditions, hysteresis, cooldowns and grouping.
- What is an escalation policy?: Escalation policies explained: steps, targets and acknowledgement timeouts, an example three-step policy, repeat rules and how to avoid alerts nobody owns.
- How to monitor DigitalOcean: How to monitor apps on DigitalOcean: droplet CPU, memory and disk, App Platform services, managed databases, load balancers, uptime checks and alerts.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.