What is Cron?
Cron is a time-based job scheduler in Unix-like systems. A "cron expression" defines when a task runs using five fields:
┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Sunday=0)
│ │ │ │ │
* * * * *
Each field can be a specific value, a range, a list, or a wildcard.
Basic Examples
| Expression | Meaning |
|---|---|
* * * * * | Every minute |
0 * * * * | Every hour (at minute 0) |
0 0 * * * | Every day at midnight |
0 9 * * 1 | Every Monday at 9:00 AM |
0 0 1 * * | First day of every month at midnight |
30 4 * * * | Every day at 4:30 AM |
0 0 * * 0 | Every Sunday at midnight |
Special Characters
| Character | Meaning | Example |
|---|---|---|
* | Any value | * * * * * = every minute |
, | List of values | 0,30 * * * * = at 0 and 30 minutes |
- | Range | 0 9-17 * * * = hourly, 9 AM to 5 PM |
/ | Step values | */15 * * * * = every 15 minutes |
Common Patterns
# Every 15 minutes
*/15 * * * *
# Every hour at minute 30
30 * * * *
# Twice daily at 6 AM and 6 PM
0 6,18 * * *
# Weekdays at 9 AM
0 9 * * 1-5
# First Monday of each month at noon
0 12 1-7 * 1
# Every 6 hours
0 */6 * * *
# Last day of month (approximation - 28th)
0 0 28 * *
Where You'll See This
- Linux crontab - System task scheduling
- GitHub Actions -
scheduletrigger - AWS CloudWatch Events - Lambda scheduling
- Kubernetes CronJobs - Container scheduling
- CI/CD pipelines - Scheduled builds
- Database jobs - Backups, cleanup tasks
Crontab Basics
# Edit your crontab
crontab -e
# List current jobs
crontab -l
# Example crontab entry
# Run backup script every day at 2 AM
0 2 * * * /home/user/backup.sh
# With output logging
0 2 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
Cron runs with a minimal environment. Always use full paths for commands and scripts. Set PATH at the top of your crontab if needed.
Special Strings
Some cron implementations support readable shortcuts:
| String | Equivalent | Meaning |
|---|---|---|
@yearly | 0 0 1 1 * | Once a year (Jan 1st) |
@monthly | 0 0 1 * * | Once a month (1st) |
@weekly | 0 0 * * 0 | Once a week (Sunday) |
@daily | 0 0 * * * | Once a day (midnight) |
@hourly | 0 * * * * | Once an hour |
@reboot | — | On system startup |
Extended Cron (6 fields)
Some systems add a seconds field at the beginning:
┌───────────── second (0-59)
│ ┌───────────── minute (0-59)
│ │ ┌───────────── hour (0-23)
│ │ │ ┌───────────── day of month (1-31)
│ │ │ │ ┌───────────── month (1-12)
│ │ │ │ │ ┌───────────── day of week (0-6)
│ │ │ │ │ │
* * * * * *
Used by: Spring, Quartz, some cloud services.
Common Gotchas
Cron typically uses server timezone. When scheduling for users in different timezones, or for cloud services, check which timezone applies. UTC is safest.
- Day of month AND day of week - If both are set (not
*), the job runs when EITHER matches, not both. - No seconds - Standard cron is minute-precision. Need seconds? Use extended cron or a different scheduler.
- Missed jobs - If the system is off when a job should run, it won't run retroactively.
- Overlapping runs - If a job takes longer than its interval, you may get concurrent executions. Use file locks.
Testing Cron Expressions
Before deploying, verify your expression:
# See next 5 run times (requires croniter or similar)
pip install croniter
python -c "
from croniter import croniter
from datetime import datetime
c = croniter('0 9 * * 1-5', datetime.now())
for _ in range(5):
print(c.get_next(datetime))
"
Or use an online tool to visualize when your cron will run.
Try It
Build Cron Expression"There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors in cron expressions."