Cron Expressions — A Practical Guide with Examples & Next Run Times
Master cron expressions for scheduling tasks. Learn syntax, examples, and how to parse cron expressions with our free online tool.
What Is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It allows users to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals.
Cron expressions are the patterns used to define these schedules.
Cron Expression Format
A standard cron expression has 5 fields (some implementations have 6 or 7):
* * * * *
│ │ │ │ │
│ │ │ │ └─── Day of week (0-7, where 0 and 7 = Sunday)
│ │ │ └───── Month (1-12)
│ │ └─────── Day of month (1-31)
│ └───────── Hour (0-23)
└─────────── Minute (0-59)
Some systems (like Quartz) use 6-7 fields:
* * * * * * *
│ │ │ │ │ │ │
│ │ │ │ │ │ └─── Year (optional)
│ │ │ │ │ └───── Day of week (0-6, where 0 = Sunday)
│ │ │ │ └─────── Month (1-12 or JAN-DEC)
│ │ │ └───────── Day of month (1-31)
│ │ └─────────── Hour (0-23)
│ └───────────── Minute (0-59)
└─────────────── Second (0-59)
Special Characters
| Character | Meaning | Example |
|---|---|---|
* | Any value | * * * * * = every minute |
, | List separator | 1,15,30 * * * * = at minutes 1, 15, and 30 |
- | Range | 1-10 * * * * = minutes 1 through 10 |
/ | Step values | */15 * * * * = every 15 minutes |
? | No specific value (Quartz) | Used in day-of-month or day-of-week |
L | Last (Quartz) | L in day-of-month = last day of month |
W | Weekday (Quartz) | 15W = nearest weekday to 15th |
# | Nth weekday (Quartz) | 5#3 = 3rd Friday (5 = Friday, 3 = 3rd) |
Common Cron Examples
Every X Minutes/Hours
| Schedule | Cron Expression |
|---|---|
| Every minute | * * * * * |
| Every 5 minutes | */5 * * * * |
| Every 15 minutes | */15 * * * * |
| Every 30 minutes | */30 * * * * |
| Every hour | 0 * * * * |
| Every 2 hours | 0 */2 * * * |
| Every 6 hours | 0 */6 * * * |
Specific Times
| Schedule | Cron Expression |
|---|---|
| Daily at midnight | 0 0 * * * |
| Daily at 3:30 AM | 30 3 * * * |
| Daily at 8 AM and 8 PM | 0 8,20 * * * |
| Every Sunday at midnight | 0 0 * * 0 |
| Every weekday at 9 AM | 0 9 * * 1-5 |
| 1st of every month at midnight | 0 0 1 * * |
| Every 15th at noon | 0 12 15 * * |
Complex Schedules
| Schedule | Cron Expression |
|---|---|
| Every weekday at 9 AM | 0 9 * * 1-5 |
| Every 10 minutes during business hours (9 AM-5 PM) | */10 9-17 * * 1-5 |
| Twice a day (8 AM and 6 PM) on weekends | 0 8,18 * * 0,6 |
| First Monday of every month at 10 AM | 0 10 1-7 * 1 (approximate*) |
| Quarterly on the 1st at midnight | 0 0 1 1,4,7,10 * |
*Note: "First Monday" is tricky in standard cron. Some systems use 1-7 with 1 (Monday) to approximate.
Using FreeToolJet's Cron Parser
Our Cron Parser tool helps you understand and validate cron expressions:
Step-by-Step Guide
- Open the Cron Parser tool
- Enter a cron expression (e.g.,
0 9 * * 1-5) - View the human-readable description ("At 09:00 AM, Monday through Friday")
- See the next 10 execution times
- Verify the expression is valid
Features
- Human-readable output: Translates cron to plain language
- Next run times: Shows when the job will next execute
- Validation: Catches syntax errors
- Multiple formats: Supports standard 5-field and Quartz 6-7 field formats
- Timezone support: Shows next runs in your local timezone
Cron in Different Systems
Linux/macOS Crontab
# Open crontab editor
# View current crontab crontab -l
# Remove crontab
crontab -r
`
Example crontab entries:
`bash
# Run backup.sh every day at 2 AM
0 2 * * * /home/user/backup.sh
# Run script every 10 minutes */10 * * * * /path/to/script.sh
# Run at reboot
@reboot /path/to/script.sh
`
Special Strings (Linux Crontab)
| String | Meaning | Equivalent To |
|---|---|---|
@reboot | Run once at startup | - |
@yearly or @annually | Once a year | 0 0 1 1 * |
@monthly | Once a month | 0 0 1 * * |
@weekly | Once a week | 0 0 * * 0 |
@daily or @midnight | Once a day | 0 0 * * * |
@hourly | Once an hour | 0 * * * * |
Kubernetes CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: backup-job
spec:
schedule: "0 2 * * *" # Daily at 2 AM
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: backup-image:latest
command: ["/bin/sh", "-c", "/backup.sh"]
restartPolicy: OnFailure
Quartz Scheduler (Java)
Quartz uses 6-7 fields:
// Every 10 seconds
// Every day at 9 AM String cron = "0 0 9 * * ?";
// Every weekday at 9 AM
String cron = "0 0 9 ? * MON-FRI";
`
Note: Quartz uses ? for "no specific value" in day-of-month or day-of-week fields.
Cron Expression Generator
If you're having trouble writing a cron expression, use our mental model:
"At X minutes past the hour..."
At 30 minutes past the hour → 30 * * * *
"Every X time units..."
Every 15 minutes → */15 * * * *
Every 2 hours → 0 */2 * * *
Every 3 days → 0 0 */3 * *
"At a specific time on specific days..."
At 9 AM on weekdays → 0 9 * * 1-5
At midnight on the 1st of every month → 0 0 1 * *
Common Pitfalls
1. Day of Week Confusion
In standard cron:
- 0 = Sunday
- 1-6 = Monday-Saturday
- 7 = Sunday (same as 0)
In some systems:
- 0 = Sunday (can also be Monday in some non-standard implementations)
Always test your cron expressions!
2. February 29th
# This will NOT run on Feb 29 (leap year)
0 0 29 2 * /path/to/script.sh
If you need a task to run on Feb 29, you'll need to check for leap year in your script.
3. Daylight Saving Time
Cron uses local time. During DST transitions: - Spring forward: A job scheduled at 2:30 AM won't run (time jumps from 2:00 to 3:00) - Fall back: A job scheduled at 1:30 AM will run twice
Workaround: Use UTC time in your cron expressions to avoid DST issues.
4. Overlapping Jobs
If a job takes longer than its interval, multiple instances might run simultaneously.
Fix: Use file locking:
#!/bin/bash
(
flock -n 200 || exit 1
# Your job here
) 200>/var/lock/myjob.lock
5. Environment Variables
Cron runs with a minimal environment. Scripts that work in your terminal might fail in cron.
Fix: Use absolute paths and set environment variables in the crontab:
PATH=/usr/local/bin:/usr/bin:/bin
0 2 * * * /path/to/script.sh
Debugging Cron Jobs
1. Check Cron Logs
# Linux
# macOS
tail -f /var/log/system.log | grep cron
`
2. Redirect Output
# Log stdout and stderr
0 2 * * * /path/to/script.sh >> /tmp/cron.log 2>&1
3. Test Cron Environment
# Add to crontab
* * * * * env > /tmp/cron-env.log
Then check /tmp/cron-env.log to see the cron environment.
4. Run as the Cron User
Cron jobs run as the user who owns the crontab. Test with:
sudo -u username /path/to/script.sh
Cron Security Best Practices
- Use absolute paths: Never rely on
$PATH - Set permissions correctly: Cron scripts should not be world-writable
- Validate input: If cron job processes external data, validate it
- Limit environment: Don't export sensitive environment variables
- Monitor execution: Set up alerts for failed cron jobs
- Use least privilege: Run cron jobs as a dedicated user, not root
Alternatives to Cron
| Tool | Description | When to Use |
|---|---|---|
| systemd timers | Modern Linux alternative to cron | Linux systems with systemd |
| APScheduler | Python task scheduler | Python applications |
| node-cron | Node.js cron implementation | Node.js applications |
| Temporal | Workflow orchestration | Complex, long-running tasks |
| Airflow | Data pipeline scheduler | Data engineering workflows |
| Cloud scheduler | Managed cron service | Cloud-native applications |
Tips for Writing Great Cron Expressions
- Test with a short interval first: Use
* * * * *(every minute) to verify your script works - Use descriptive comments: Add comments in your crontab explaining each job
- Log everything: Redirect output to log files for debugging
- Handle errors gracefully: Your script should handle failures and clean up
- Use atomic operations: Ensure partial failures don't leave data in an inconsistent state
- Consider timezone: Specify timezone explicitly if your server might move
Related Tools
- Cron Parser — Parse and validate cron expressions
- Timestamp Converter — Convert between timestamps and dates
- JSON Formatter — Format JSON configuration files