Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

crontab

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?

like image 411
Nikola Avatar asked May 03 '13 05:05

Nikola


People also ask

Do cron jobs run in order?

For completeness, jobs in e.g. /etc/cron. {hourly,daily,weekly,monthly} are run sequentially. The run-parts script loops over all files in this directory. So you can use that in combination with a naming convention (similar to the numbering in /etc/init.

What does 30 * * * * mean in crontab?

*/30 * * * * your_command. this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc) example #3. 0,30 * * * * your_command. this means "run when the minute of each hour is 0 or 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)

Can cronjobs run at the same time?

Yes, cronjobs can run at the same time, and will do so if you set them up that way. A 1 minute gap between each of the jobs might work, but what if one of them takes longer than a minute to run?


2 Answers

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.

like image 118
Alex Avatar answered Oct 20 '22 13:10

Alex


@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
like image 22
Peter Gibson Avatar answered Oct 20 '22 14:10

Peter Gibson