Back to Blog
Guides
cron job monitoring
scheduled task monitoring

Cron Job Monitoring: Never Miss a Failed Scheduled Task

Learn how to monitor cron jobs effectively. Get alerts when scheduled tasks fail, run late, or don't run at all. Comprehensive guide for Linux cron monitoring.

WT

Wakestack Team

Engineering Team

6 min read

Who This Is For

This guide is for system administrators and developers who manage cron jobs on Linux servers. If you've ever discovered a critical backup or sync job failed days ago, this guide will help you never miss failures again.

The Problem with Cron Jobs

Silent Failures

Cron jobs fail silently by default:

# This backup might fail and you'd never know
0 2 * * * /scripts/backup.sh

Common failure modes:

  • Script errors
  • Disk full
  • Network timeouts
  • Permission issues
  • Missing dependencies

Delayed Discovery

Without monitoring:

  1. Day 1: Backup fails
  2. Day 2-7: More backups fail
  3. Day 8: Disaster strikes
  4. Day 8: "Where are the backups?" 😱

No Built-in Monitoring

Linux cron has minimal monitoring:

  • MAILTO sends to local mailbox (rarely checked)
  • Exit codes aren't tracked
  • No central dashboard
  • No alerting integration

How Cron Job Monitoring Works

The Heartbeat Pattern

  1. Create a heartbeat URL for each cron job
  2. Add a ping to your cron job on success
  3. Monitor expects ping within schedule
  4. Missing ping = alert
# Before: Unmonitored
0 2 * * * /scripts/backup.sh
 
# After: Monitored
0 2 * * * /scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup

What Gets Detected

ScenarioDetection
Script crashesNo heartbeat → Alert
Job never startsNo heartbeat → Alert
Job runs lateLate heartbeat → Alert
Job takes too longDuration tracking
Job succeedsHeartbeat received ✓

Setting Up Cron Job Monitoring

Step 1: Create Heartbeat Monitor

In Wakestack:

  1. Add Monitor → Heartbeat type
  2. Name it (e.g., "Nightly Backup")
  3. Set schedule:
    Expected: Every 24 hours
    Grace period: 1 hour
    
  4. Copy the unique URL

Step 2: Update Your Crontab

# Edit crontab
crontab -e

Add heartbeat ping after your job:

# Database backup - runs at 2 AM, pings on success
0 2 * * * /scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup
 
# Data sync - runs every hour
0 * * * * /scripts/sync.sh && curl -fsS https://ping.wakestack.co.uk/hb/sync
 
# Weekly report - runs Sunday at midnight
0 0 * * 0 /scripts/report.sh && curl -fsS https://ping.wakestack.co.uk/hb/report

Step 3: Test the Setup

Run your job manually:

/scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup

Verify heartbeat received in Wakestack dashboard.

Step 4: Configure Alerts

Set notification channels:

  • Email for all failures
  • Slack for team visibility
  • PagerDuty for critical jobs

Cron Job Monitoring Best Practices

1. Only Ping on Success

Use && to ping only when the job succeeds:

# Good: Only pings if backup succeeds
0 2 * * * /scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup
 
# Bad: Pings even if backup fails
0 2 * * * /scripts/backup.sh; curl -fsS https://ping.wakestack.co.uk/hb/backup

2. Handle Exit Codes Properly

Ensure your scripts exit with proper codes:

#!/bin/bash
set -e  # Exit on any error
 
pg_dump mydb > backup.sql
aws s3 cp backup.sql s3://backups/
 
# Script exits 0 on success, non-zero on failure

3. Set Appropriate Grace Periods

Account for job duration variability:

Job DurationGrace Period
< 1 minute5-10 minutes
1-10 minutes15-30 minutes
10-60 minutes1-2 hours
1+ hours2-3 hours

4. Add Logging

Log job execution alongside heartbeats:

#!/bin/bash
LOG=/var/log/backup.log
echo "$(date): Starting backup" >> $LOG
 
if /scripts/backup.sh >> $LOG 2>&1; then
    echo "$(date): Backup succeeded" >> $LOG
    curl -fsS https://ping.wakestack.co.uk/hb/backup
else
    echo "$(date): Backup failed with exit code $?" >> $LOG
    exit 1
fi

5. Use Unique Heartbeats Per Job

Don't share heartbeat URLs between different jobs:

# Each job gets its own heartbeat
0 2 * * * /scripts/backup-db.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup-db
0 3 * * * /scripts/backup-files.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup-files
0 * * * * /scripts/sync-data.sh && curl -fsS https://ping.wakestack.co.uk/hb/sync-data

Common Cron Job Patterns

Pattern 1: Simple Job

# Job runs, ping on success
0 2 * * * /scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup

Pattern 2: With Timeout

# Timeout after 30 minutes
0 2 * * * timeout 1800 /scripts/backup.sh && curl -fsS https://ping.wakestack.co.uk/hb/backup

Pattern 3: With Locking

# Prevent overlapping runs
0 * * * * flock -n /tmp/sync.lock /scripts/sync.sh && curl -fsS https://ping.wakestack.co.uk/hb/sync

Pattern 4: With Retry Logic

#!/bin/bash
# retry-backup.sh
 
for i in 1 2 3; do
    if /scripts/backup.sh; then
        curl -fsS https://ping.wakestack.co.uk/hb/backup
        exit 0
    fi
    sleep 300  # Wait 5 min before retry
done
 
exit 1  # All retries failed, no heartbeat sent

Pattern 5: Duration Tracking

#!/bin/bash
START=$(date +%s)
/scripts/backup.sh
EXIT_CODE=$?
END=$(date +%s)
DURATION=$((END-START))
 
if [ $EXIT_CODE -eq 0 ]; then
    curl -fsS "https://ping.wakestack.co.uk/hb/backup?duration=${DURATION}"
fi
exit $EXIT_CODE

Troubleshooting Cron Job Monitoring

Heartbeat Not Received

Check 1: Is the job running?

# Check cron logs
grep CRON /var/log/syslog | tail -20

Check 2: Is the job succeeding?

# Run manually
/scripts/backup.sh
echo "Exit code: $?"

Check 3: Can the server reach the heartbeat URL?

curl -v https://ping.wakestack.co.uk/hb/test

Check 4: Is curl installed?

which curl
# Or use wget instead
wget -q -O /dev/null https://ping.wakestack.co.uk/hb/backup

Job Runs But Heartbeat Fails

Add debugging:

0 2 * * * /scripts/backup.sh && curl -fsS -w "\\n%{http_code}\\n" https://ping.wakestack.co.uk/hb/backup >> /var/log/heartbeat.log 2>&1

Too Many False Alerts

  • Increase grace period
  • Check for network reliability issues
  • Add retry logic to curl:
curl -fsS --retry 3 --retry-delay 5 https://ping.wakestack.co.uk/hb/backup

Cron Job Monitoring Checklist

Before going live:

  • Heartbeat URL created for each job
  • Schedule matches cron schedule
  • Grace period accounts for duration
  • Ping only on success (&& not ;)
  • Alert channels configured
  • Test run completed successfully
  • Alerts tested manually

Wakestack Cron Job Monitoring

Monitor your scheduled tasks with heartbeat monitoring.

Features:

  • Unique URLs per job
  • Configurable schedules
  • Grace periods
  • Duration tracking
  • Multi-channel alerts
  • Dashboard visibility

Pricing:

  • Free: 5 monitors (heartbeats count)
  • Pro: 50 monitors, $29/mo
  • Enterprise: Unlimited, $99/mo

Monitor Your Cron Jobs →

About the Author

WT

Wakestack Team

Engineering Team

Frequently Asked Questions

How do I monitor cron jobs?

Use heartbeat monitoring: add a curl command to your cron job that pings a unique URL when the job completes. If the ping doesn't arrive on schedule, you get alerted.

Why do cron jobs fail silently?

Cron jobs fail silently because there's no built-in notification system. By default, cron only sends email (often to a local mailbox nobody checks). External monitoring catches these failures.

Can I monitor cron jobs for free?

Yes, Wakestack's free tier includes heartbeat monitoring for cron jobs. You get 5 monitors which can include cron job heartbeats.

Related Articles

Ready to monitor your uptime?

Start monitoring your websites, APIs, and services in minutes. Free forever for small projects.