What is a Cron Job?
A cron job is a command or script that runs automatically at specified times or intervals. The name "cron" comes from Chronos, the Greek god of time.
The cron daemon (crond) runs in the background on Unix/Linux systems, waking up every minute to check if any scheduled tasks need to run. If a task is scheduled for the current time, cron runs it.
Common uses for cron jobs: • Database backups (run at 2 AM every night) • Sending scheduled email reports (every Monday morning) • Clearing temporary files (every hour) • Checking website uptime and alerting on failures (every 5 minutes) • Importing data from an external API (every 30 minutes) • Renewing SSL certificates (twice daily — this is how Let's Encrypt works) • Running database maintenance queries (every Sunday at midnight)
The Cron Expression Format
A cron expression has five fields, each representing a time unit:
┌───────────── minute (0–59) │ ┌───────────── hour (0–23) │ │ ┌───────────── day of month (1–31) │ │ │ ┌───────────── month (1–12) │ │ │ │ ┌───────────── day of week (0–6, Sunday=0) │ │ │ │ │ * * * * * command to execute
Special characters: • * (asterisk) — any value ("every") • , (comma) — multiple values (1,3,5 = 1st, 3rd and 5th) • - (hyphen) — range (1-5 = 1 through 5) • / (slash) — step values (*/15 = every 15 units)
Common Cron Expression Examples
Reading cron expressions becomes natural with practice. Here are the most useful patterns:
0 * * * * — Every hour at :00 (top of the hour) 0 0 * * * — Every day at midnight 0 9 * * 1 — Every Monday at 9:00 AM 0 2 * * * — Every night at 2:00 AM */5 * * * * — Every 5 minutes 0 9-17 * * 1-5 — Every hour from 9 AM to 5 PM, Monday to Friday 0 0 1 * * — First day of every month at midnight 0 0 * * 0 — Every Sunday at midnight 30 4 1,15 * * — At 4:30 AM on the 1st and 15th of every month 0 */4 * * * — Every 4 hours
Special strings (supported by most cron implementations): @yearly — 0 0 1 1 * (once a year) @monthly — 0 0 1 * * (once a month) @weekly — 0 0 * * 0 (once a week) @daily — 0 0 * * * (once a day) @hourly — 0 * * * * (once an hour)
Cron in GitHub Actions, AWS and Beyond
Cron expressions are not just for Linux servers. They are used in modern cloud infrastructure:
GitHub Actions uses cron syntax in workflow files:
on: schedule: - cron: '0 9 * * 1' # Every Monday at 9 AM UTC
AWS EventBridge (CloudWatch Events) uses a slightly different format with a 6th field for year. AWS also supports a "rate" syntax: rate(5 minutes).
Kubernetes CronJobs use standard cron expressions to schedule containerized tasks.
Heroku Scheduler uses a simplified interface but generates standard cron expressions.
Important note: cron jobs on servers run in UTC by default. Always confirm your server's timezone and account for it in your expressions. A job meant to run at 9 AM IST (Indian Standard Time) is UTC+5:30, so the cron expression should use hour 3 (9 - 5 = 3, rounded).
Debugging and Testing Cron Jobs
Cron jobs are notoriously hard to debug because they run in a minimal environment — often without the PATH variables, environment variables or working directory your script expects.
Best practices for reliable cron jobs:
- 1Use absolute paths — instead of "python script.py", use "/usr/bin/python3 /home/user/scripts/script.py". Cron runs with a minimal PATH and may not find commands otherwise.
- 1Redirect output to a log file — add >> /var/log/myjob.log 2>&1 to capture both stdout and stderr. Without this, failed jobs produce no visible output.
- 1Test with a nearby time first — set the cron to run 2 minutes in the future, verify it runs correctly, then change to the actual schedule.
- 1Check cron logs — on most Linux systems, cron activity is logged to /var/log/syslog or /var/log/cron.
- 1Use a cron expression builder — visually construct expressions and see plain-English descriptions before deploying them to production.