Cron overnight from 22:00 to 06:00: why 22-6 does not work
Cron ranges cannot wrap around the end of a field, so 0 22-6 * * * is an error. Write the overnight window as two pieces: 22,23,0-6.
At a glance
- Expression
- 0 22,23,0-6 * * *
- Fires
- 9 times a night, 63 times a week.
- Restricted fields
- Minute pinned to 0, hour restricted to nine values. Date fields open.
- Equivalent macro
- None.
- Hours matched
- 0, 1, 2, 3, 4, 5, 6, 22, 23. Nine values.
- Why 22-6 fails
- A range requires the start to be less than or equal to the end, so 22-6 is invalid rather than wrapping.
In detail
Every cron range is evaluated as a simple walk from the start value to the end value, which means the start has to come first. 22-6 asks the parser to count upwards from 22 and stop at 6, which never terminates, so it is rejected as an invalid range instead of being reinterpreted as an overnight span.
The fix is to describe the window as the two pieces it actually is: the part before midnight and the part after. 22,23,0-6 covers hours 22 and 23 of one day and hours 0 through 6 of the next, and the comma list joins them into a single field. Nine hours, nine runs a night at the top of each hour.
It is worth being explicit that this is one schedule spanning two calendar dates, not two schedules. The runs at 22:00 and 23:00 happen on Monday night and the runs at 00:00 through 06:00 happen on Tuesday morning. Any day of week restriction applies to the actual date of each run, so 0 22,23,0-6 * * 1 gives a very odd looking Monday only window.
If the window genuinely needs to follow a single night rather than a clock pattern, express it as two crontab lines with different day of week restrictions: one for the evening part on the nights you want, and one for the morning part on the following days. That is more verbose but it is the only way to say Friday night meaning Friday evening plus Saturday morning.
Examples
0 22,23,0-6 * * *Nine runs a night, at 22:00, 23:00, 00:00, 01:00, 02:00, 03:00, 04:00, 05:00 and 06:00.
0 22-6 * * *Rejected. A range cannot wrap past the end of the field.
0 22,23 * * 5 /opt/app/bin/night.sh
0 0-6 * * 6 /opt/app/bin/night.shThings that catch people out
- The list must have no spaces. 22,23, 0-6 splits into extra tokens and changes the field count.
- A day of week restriction on a wrapped window applies per run, not per night, which is almost never what people expect.
- 23-0 is also invalid for the same reason. There is no value of a single range that spans midnight.
Try it: Cron Expression Parser
Paste any five or six field expression to get a plain English description, a per field breakdown and the next run times in your local time.
Cron Expression Parser
LOCAL · Browser-OnlyParse cron expressions and see the next scheduled run times
Supports 5-field (minute hour day month weekday), 6-field (with seconds), named values (JAN, MON), and @macros (@daily, @hourly, ...)
Use the dropdowns below to build your cron expression visually, or type a custom value in each field.
Human-Readable Description
Every 5 minutes
Field Breakdown
| Field | Allowed | Value | Meaning |
|---|---|---|---|
| Minute | 0-59 | */5 | every 5 minute(s) |
| Hour | 0-23 | * | every hour |
| Day of Month | 1-31 | * | every day of month |
| Month | 1-12 | * | every month |
| Day of Week | 0-7 | * | every day of week |
Related cron expressions
All cron expressions →Cron during business hours: 0 9-17 * * 1-5
0 9-17 * * 1-5 runs hourly from 09:00 to 17:00 on weekdays, nine times a day and 45 times a week. The 17:00 run is included, not excluded.
The cron hyphen: inclusive ranges and why they cannot wrap
A hyphen range includes both ends, so 9-17 is nine values. The start must not be greater than the end, which is why 22-6 is rejected.
The cron comma: listing several values in one field
A comma joins several values into one field, and each element can itself be a value, a range or a step. Spaces inside a list break the expression.
The cron hour field: 0 to 23 on a 24 hour clock
The hour field runs 0 to 23 on a 24 hour clock. Midnight is 0, noon is 12, and 24 is out of range. There is no AM or PM notation.
Cron every weekday: 0 0 * * 1-5
0 0 * * 1-5 runs at midnight Monday through Friday, five times a week. Day numbers, the MON-FRI alias, and why holidays still get a run.
Cron at the weekend: 0 0 * * 0,6
0 0 * * 0,6 runs at midnight on Saturday and Sunday, twice a week. Why 6-7 works too, and why 6-0 is rejected as an invalid range.
Tools for this
- Cron Expression Parser Free online cron expression parser - parse cron expressions and see the next scheduled run times
- Crontab Generator Free online crontab generator - build cron expressions visually with an intuitive interface