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

后端 未结 3 1674
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 01:06

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

相关标签:
3条回答
  • 2021-01-18 01:38

    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.

    0 讨论(0)
  • 2021-01-18 01:45

    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

    0 讨论(0)
  • 2021-01-18 01:46

    @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
    
    0 讨论(0)
提交回复
热议问题