Nikola
Nikola

Reputation: 15048

Will crontab hour range a-b run after b too?

Say I have a crontab which runs every 20 minutes and I have a hour range which can vary so lets say a-b, which in one example could look like

*/20 5-23 * * * /usr/bin/cool_program

My question is, will the cron run at 23:00, 23:20, 23:40 and 00:00 too?

Upvotes: 11

Views: 16118

Answers (4)

Huy võ lê
Huy võ lê

Reputation: 31

I found the answer in man 5 crontab. Range is allowed.

      The time and date fields are:

              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sunday, or use names)

       A field may contain an asterisk (*), which always stands for "first-last".

       Ranges of numbers are allowed.  Ranges are two numbers separated with a hyphen.  The specified range is inclusive.  
       
       
       For example, 8-11 for an 'hours' entry specifies execution at hours 8, 9, 10, and 11. The first number must be less than or equal to the second one.

Upvotes: 0

thar45
thar45

Reputation: 3560

It will execute when minute is divisible by 20 and when hour is in 5-23 inclusive:

* 20 – every 20 minutes from 0 to 59
* 5-23 – 5 to 23 inclusive
* * – Every day
* * – Every month
* * - EvryDay of the Week

The first occurrence is 5:00 and the last 23:40

crontab.guru

Documentation for Reference

Upvotes: 5

Peter Gibson
Peter Gibson

Reputation: 19574

@Alex's answer is correct, however it took me a while to find a source.

The answer is in man crontab.5 (or also info crontab) on Debian, Mac OS X, FreeBSD (and other Posix systems):

Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an ``hours'' entry specifies execution at hours 8, 9, 10 and 11.

For my application I wanted a script to run every 5 minutes during business hours (9am - 5pm) and another to run every 5 minutes outside of that. Unfortunately the ranges can't wrap across midnight, so you need to specify 3 ranges (morning, business hours, evening)

*/5 0-8,17-23   * * *   outside-hours.sh
*/5 9-16   * * *   business-hours.sh

This should run

outside-hours.sh  first at 00:00 and finally at 08:55
business-hours.sh first at 09:00 and finally at 16:55
outside-hours.sh  first at 17:00 and finally at 23:55

Upvotes: 8

Alex
Alex

Reputation: 823

GK27's answer does not fully answer the question, so let me clarify:

cron will run jobs when the time matches the expression provided. Your expression tells it to run when the minute is divisible by 20 (*/20) and your hour range tells it to run when the hour is within the specified range inclusively (5-23). The remaining three * tell it to match any day, month, and any day of the week.

Therefore the first job will run at 05:00 because the hour, 05, is in the range 5 to 23 and the minute, 00, is divisible by 20. The last job will run at 23:40 because the hour, 23, is in the range 5 to 23 and the minute, 40, is divisible by 20. It will not run at 00:00 because the hour, 00, is not in the range 5 to 23.

Upvotes: 36

Related Questions