A cron expression is a string that defines a schedule for executing a task. If you want a schedule that spans multiple hours but starts at an arbitrary minute, you can achieve this by carefully configuring the fields of the cron expression.
Syntax Recap:
Cron expressions follow this format:
<day_of_month> <day_of_week>
minute: 0-59
hour: 0-23
day_of_month: 1-31
month: 1-12
day_of_week: 0-7 (where both 0 and 7 represent Sunday)
Problem: Arbitrary Start Minute Across Multiple Hours
To run a task every hour, starting at an arbitrary minute (e.g., 15 minutes past the hour) across multiple hours (e.g., between 10 AM and 4 PM), the solution involves specifying both the minute and hour ranges.
Example 1: Run Every Hour at :15 Between 10 AM and 4 PM
The cron expression:
15 10-16 * * *
minute: 15 — starts at 15 minutes past the hour.
hour: 10-16 — spans from 10 AM to 4 PM.
Other fields (*) are set to their defaults, meaning the job runs every day, every month, and every day of the week.
Example 2: Run Every Hour at :45 Between 8 AM and 6 PM
The cron expression:
45 8-18 * * *
minute: 45 — starts at 45 minutes past the hour.
hour: 8-18 — spans from 8 AM to 6 PM.
Other fields are unchanged.
Additional Notes
15,45 10-16 * * *
10/30 10-16 * * *