Code Reference

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

FieldValues
Minute0–59
Hour0–23
Dom1–31
Month1–12
Dow0–7 (0/7 = Sun)
*Any
*/nEvery n
a,bList
a-bRange
CommandRole
crontab -eEdit user crontab
crontab -lList
crontab -rRemove (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.sh

Wrapper script:

#!/usr/bin/env bash
set -euo pipefail
cd "$HOME/app"
/usr/bin/flock -n /tmp/app-cron.lock ./job.sh

Systemd alternative (sketch):

# app.timer + app.service with OnCalendar=*-*-* 02:30:00

⚠️ Pitfalls

  • Cron’s PATH is 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 flock to prevent pile-ups.
  • Timezone is the system timezone unless you set TZ=.
  • crontab -r deletes without confirmation on some systems.

On this page