Cron
Bash · Reference cheat sheet
Cron
Bash · Reference cheat sheet
📋 Overview
Cron schedules recurring jobs via crontab entries. Each line is five time fields plus a command. Cron runs with a minimal environment—always set PATH, use absolute paths, and redirect logs. Prefer systemd timers on modern Linux when available.
🔧 Core concepts
| Field | Values |
|---|---|
| Minute | 0–59 |
| Hour | 0–23 |
| Dom | 1–31 |
| Month | 1–12 |
| Dow | 0–7 (0/7 = Sun) |
* | Any |
*/n | Every n |
a,b | List |
a-b | Range |
| Command | Role |
|---|---|
crontab -e | Edit user crontab |
crontab -l | List |
crontab -r | Remove (dangerous) |
/etc/cron.d/ | System drop-in files |
Special strings (Vixie): @reboot, @daily, @hourly, @weekly, @monthly.
💡 Examples
User crontab entries:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
# Every day at 02:30
30 2 * * * /home/ada/bin/backup.sh >>/home/ada/logs/backup.log 2>&1
# Every 15 minutes
*/15 * * * * /usr/bin/curl -fsS https://example.com/health >/dev/null
@reboot /home/ada/bin/on-boot.shWrapper script:
#!/usr/bin/env bash
set -euo pipefail
cd "$HOME/app"
/usr/bin/flock -n /tmp/app-cron.lock ./job.shSystemd alternative (sketch):
# app.timer + app.service with OnCalendar=*-*-* 02:30:00⚠️ Pitfalls
- Cron’s
PATHis short—commands that work interactively may “not be found”. %in commands is special in crontab (newline)—escape as\%.- No TTY; don’t assume interactive prompts or ssh-agent unless configured.
- Overlapping runs: use
flockto prevent pile-ups. - Timezone is the system timezone unless you set
TZ=. crontab -rdeletes without confirmation on some systems.