Cron Jobs — Essential Guide

1 · What Is Cron?

Purpose

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”.

2 · Anatomy of a Cron Expression

Field Layout

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

Field Behaviour

SymbolMeaningExample
*Any valueRun every minute
,Value list separatorMON,WED,FRI
-Range1-5 (Mon–Fri)
/Step values*/15 (every 15 min)

3 · Special Schedule Strings

Shortcut Keywords

Syntax extensions such as @every 3h45m exist in systemd timers and Go’s cron library but are not standard Vixie Cron.

4 · Editing & Managing Crontabs

User Crontab Commands

System‑Wide Crontabs

Besides per‑user crontabs stored in /var/spool/cron/, Unix distributions provide system files:

5 · Environment & Execution Context

Key Points

A missing environment variable (e.g., LANG for UTF‑8) is a frequent source of cron bugs.

6 · Logging & Troubleshooting

Where to Look

Common Pitfalls

7 · Security & Best Practices

Guidelines

8 · Quick Reference Examples

Typical Schedules

ScheduleCron SyntaxDescription
Every 5 minutes*/5 * * * *Frequent polling/health‑check
Every weekday at 08:000 8 * * 1‑5Send daily report email
1st & 15th at 02:3030 2 1,15 * *Semi‑monthly payroll export
Last day of month 23:5555 23 28‑31 * * [ "$(date +\%d -d tomorrow)" == "01" ] && commandEnd‑of‑month tasks (Bash test inside)

Inline Examples

# 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