Cron is a time-based job‑scheduler built into most Unix‑like operating systems. It launches processes at fixed dates or intervals, making it ideal for repetitive tasks such as backups, log rotation, data processing, and system maintenance.
The name derives from chronos, the Greek word for “time”.
A standard entry contains five timing fields followed by the command to execute:
# ┌──────── min (0‑59)
# │ ┌────── hour (0‑23)
# │ │ ┌──── day‑of‑month (1‑31)
# │ │ │ ┌── month (1‑12 or JAN‑DEC)
# │ │ │ │ ┌─ day‑of‑week (0‑7 or SUN‑SAT; 0 & 7 = Sunday)
# │ │ │ │ │
# * * * * * command_to_run
Symbol | Meaning | Example |
---|---|---|
* | Any value | Run every minute |
, | Value list separator | MON,WED,FRI |
- | Range | 1-5 (Mon–Fri) |
/ | Step values | */15 (every 15 min) |
@reboot
— run once at startup@hourly
— top of every hour (0 * * * *
)@daily
— midnight every day (0 0 * * *
)@weekly
— midnight Sunday (0 0 * * 0
)@monthly
— midnight 1st of month (0 0 1 * *
)@yearly
/@annually
— midnight Jan 1 (0 0 1 1 *
)
Syntax extensions such as @every 3h45m
exist in systemd timers and
Go’s cron
library but are not standard Vixie Cron.
Besides per‑user crontabs stored in /var/spool/cron/
,
Unix distributions provide system files:
/etc/crontab
(adds a user
field: m h dom mon dow user command
)/etc/cron.d
or /etc/cron.*
$PATH
is minimal (/usr/bin:/bin
). Set explicit paths or export variables at top of crontab./bin/sh
unless SHELL=/bin/bash
is specified.>/var/log/job.log 2>&1
).
A missing environment variable (e.g., LANG
for UTF‑8) is a frequent source of cron bugs.
/var/log/cron
or journalctl (journalctl -u cron
)MAILTO=you@example.com
in crontab to receive job output/bin/sh
).chmod +x
) and readable paths.flock
or test PID files to avoid duplicates.root
.# Backup /var/www to S3
).Schedule | Cron Syntax | Description |
---|---|---|
Every 5 minutes | */5 * * * * | Frequent polling/health‑check |
Every weekday at 08:00 | 0 8 * * 1‑5 | Send daily report email |
1st & 15th at 02:30 | 30 2 1,15 * * | Semi‑monthly payroll export |
Last day of month 23:55 | 55 23 28‑31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && command | End‑of‑month tasks (Bash test inside) |
# Backup /home at 01:00 daily
0 1 * * * /usr/local/bin/backup.sh
# Clear system cache every Sunday at 03:15
15 3 * * 0 /usr/bin/sync ; /usr/bin/sysctl -w vm.drop_caches=3
# Run Python script at reboot
@reboot /usr/bin/python3 /opt/app/startup.py >>/var/log/app.log 2>&1