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

旧巷老猫 提交于 2019-12-01 17:29:26

Yes offcourse it will execute on every 2o mins from 5am till 23 hrs,

* 20 – 20th Minute (Top of the hour)
* 5-23 – 5.20 am,5.40 am,6 am,....23 pm
* * – Every day
* * – Every month
* * - EvryDay of the Week

Documentation for Reference

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.

The other answers are 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!