Cron Job
A cron job is an alarm clock for your server. You tell it “run this script every day at 3 AM” or “check for updates every Sunday at midnight,” and it does it automatically without anyone needing to be awake. It is the simplest form of automation on Linux systems, handling everything from database backups to log cleanup to sending scheduled reports.
Cron is a time-based job scheduler in Unix-like operating systems. The cron daemon (crond) runs in the background and executes commands at scheduled times defined in crontab (cron table) files.
Crontab syntax (five fields + command):
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * * commandSpecial characters:
*= every value,= list (1,3,5)-= range (1-5)/= step (*/5 = every 5 units)
Common schedules:
| Expression | Meaning |
|---|---|
0 3 * * * | Daily at 3:00 AM |
*/15 * * * * | Every 15 minutes |
0 0 * * 0 | Every Sunday at midnight |
0 0 1 * * | First day of every month |
30 6 * * 1-5 | Weekdays at 6:30 AM |
Crontab locations:
crontab -e: per-user crontab/etc/crontab: system-wide crontab (includes user field)/etc/cron.d/: drop-in cron files/etc/cron.daily/,/etc/cron.weekly/,/etc/cron.monthly/: run-parts directories
Modern alternative: systemd timers offer more flexibility (calendar events, randomized delays, dependency ordering) and better logging integration.
Cron job management
# Edit your crontab
$ crontab -e
# Example crontab entries
# Backup database every night at 2 AM
0 2 * * * /opt/scripts/backup-db.sh >> /var/log/backup.log 2>&1
# Clean old logs every Sunday at 4 AM
0 4 * * 0 find /var/log -name "*.log" -mtime +30 -delete
# Health check every 5 minutes
*/5 * * * * curl -sf https://hc-ping.com/abc123 > /dev/null
# ZFS scrub on the first Sunday of each month
0 1 1-7 * 0 /sbin/zpool scrub tank
# List current crontab
$ crontab -l
# View cron execution logs
$ grep CRON /var/log/syslog | tail -20 Cron jobs handle essential background tasks on virtually every Linux server: rotating logs, running backups, renewing TLS certificates (certbot auto-renewal), updating DNS records, and executing monitoring checks. The most common cron mistakes are: forgetting to redirect output (cron emails stdout/stderr to the user, filling up mailboxes), not using absolute paths (cron runs with a minimal PATH), and not handling overlapping runs (a job that takes 10 minutes scheduled every 5 minutes). Health check services like Healthchecks.io and Cronitor monitor cron job execution: the job pings a URL on success, and you get alerted if the ping is late. For complex scheduling needs, systemd timers or dedicated job schedulers (Airflow, Celery Beat) provide better logging, retry logic, and dependency management.