How to Parse Cron Expressions
Understand and interpret cron schedule expressions to determine when a job runs.
Problem
You have a cron expression and need to understand the schedule it defines.
Prerequisites
- Understanding of cron fields
- Knowledge of time-based scheduling
Steps
-
1. Learn the five fields
A standard cron expression has five fields: minute (0-59), hour (0-23), day of month (1-31), month (1-12), and day of week (0-6, where 0 is Sunday). Each field can be a value, range, list, step (*/5), or wildcard (*).
Input30 2 * * *Outputminute=30, hour=2, every day -
2. Interpret the expression
Read each field to determine the schedule. '30 2 * * *' means 2:30 AM every day. '*/15 * * * *' means every 15 minutes. '0 9 * * 1-5' means 9:00 AM Monday through Friday. Special strings like @daily provide common shortcuts.
Input0 9 * * 1-5Output9:00 AM, Monday-Friday -
3. Calculate the next run time
Given the cron expression and a current time, compute the next time the job will execute. This requires evaluating all five fields together, handling month lengths, and resolving day-of-month vs day-of-week interaction (OR logic in standard cron).
Input0 9 * * 1-5, from: 2023-10-14 10:00Outputnext: 2023-10-16 09:00 (Monday)
Expected result
A clear understanding of the cron schedule and the ability to compute next run times.
Common errors
- Day of week and day of month interaction
- In standard cron, if both day-of-month and day-of-week are restricted (not *), the job runs when EITHER matches (OR logic). Some implementations use AND logic. Check your implementation.
- Using 7 for Sunday
- Some cron implementations accept 7 for Sunday in addition to 0. Others only accept 0. Check your implementation's convention.
Troubleshooting
- If the next run time is wrong, verify the day-of-month/day-of-week interaction logic.
- Step values (*/N) start at the field minimum, not at 1 for all fields.
Security limits
- Parsing cron expressions is a read-only analysis. No security implications.
Related tools
Related tasks
Ready to run it?