Cron Jobs
Schedule recurring tasks with cron — backups, cleanup, reports, and maintenance
Free tier: read the explanation here. Upgrade to Pro for Drills, Speed challenges & Mastery badges.
UpgradeKnowledge Debt detected
You can study this freely — but your score may plateau if these foundations have gaps. The Mastery badge requires them to be solid.
Explanation
Cron is the standard Unix scheduler. It runs commands at specified times — automatically, without human intervention. Every production server uses cron for backups, log rotation, health checks, and cleanup tasks.
crontab — editing your cron jobs:
bash crontab -e # edit your crontab (opens in $EDITOR) crontab -l # list your current cron jobs crontab -r # remove all your cron jobs (careful!)
Cron syntax — 5 fields + command:
* * * * * command to run | | | | | | | | | +--- day of week (0-7, 0 and 7 = Sunday) | | | +----- month (1-12) | | +------- day of month (1-31) | +--------- hour (0-23) +----------- minute (0-59)
Common patterns:
# Every minute * * * * * /usr/bin/check-health.sh # Every 15 minutes */15 * * * * /usr/bin/sync-data.sh # Every hour at :30 30 * * * * /usr/bin/report.sh # Every day at 2:00 AM 0 2 * * * /usr/bin/backup.sh # Every day at 2:00 AM, Mon-Fri only 0 2 * * 1-5 /usr/bin/weekday-task.sh # Every Sunday at midnight 0 0 * * 0 /usr/bin/weekly-cleanup.sh # First day of each month at 3:00 AM 0 3 1 * * /usr/bin/monthly-report.sh
Shorthand aliases:
@reboot /usr/bin/start-app.sh # runs once at system startup @hourly /usr/bin/sync.sh # = 0 * * * * @daily /usr/bin/backup.sh # = 0 0 * * * @weekly /usr/bin/report.sh # = 0 0 * * 0 @monthly /usr/bin/archive.sh # = 0 0 1 * *
Critical: always use absolute paths in cron:
bash # WRONG — cron has a minimal PATH; "python" may not be found * * * * * python /home/bill/script.py # CORRECT — use the full path * * * * * /usr/bin/python3 /home/bill/script.py # Find the full path with: which python3
Capturing output:
bash # By default, cron emails output to the local user. Redirect to a file instead: 0 2 * * * /usr/bin/backup.sh >> /var/log/backup.log 2>&1 # >> appends, 2>&1 captures stderr into the same file # Suppress all output (silent cron) 0 2 * * * /usr/bin/cleanup.sh > /dev/null 2>&1
Examples
Setting up daily database backups
Always test your cron commands manually first — cron will not tell you if they are broken.
# Edit your crontab
crontab -e
# Add these lines:
# Daily backup at 2:00 AM
0 2 * * * /usr/bin/pg_dump mydb > /backups/mydb_$(date +\%Y\%m\%d).sql 2>> /var/log/backup.log
# Weekly cleanup: delete backups older than 30 days
0 3 * * 0 /usr/bin/find /backups/ -name "*.sql" -mtime +30 -delete >> /var/log/backup.log 2>&1
# Verify it was saved
crontab -l
# Test your backup script works before relying on cron
/usr/bin/pg_dump mydb > /backups/test_backup.sql
ls -lh /backups/test_backup.sql
# -rw-r--r-- 1 bill bill 4.2M Jun 27 test_backup.sqlHealth check every 5 minutes
Health check scripts are one of the most practical uses of cron. Every production service should have one.
#!/bin/bash
# health_check.sh
set -e
URL="https://myapp.com/health"
LOG="/var/log/health-check.log"
# Timestamp
echo "$(date '+%Y-%m-%d %H:%M:%S') - Checking $URL" >> $LOG
# curl: -s silent, -o /dev/null discard body, -w print status code
STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$URL")
if [ "$STATUS" != "200" ]; then
echo "$(date) - ALERT: $URL returned $STATUS" >> $LOG
exit 1
fi
echo "$(date) - OK: $STATUS" >> $LOG
# crontab entry:
# */5 * * * * /home/bill/health_check.sh >> /var/log/health-check.log 2>&1How well did you understand this?