Run a cron job every minute only on specific hours?

后端 未结 2 2056
时光取名叫无心
时光取名叫无心 2020-12-31 01:00

How do you run a cron job every minute only on specific hours? Like so:

It will only check every minute from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM<

相关标签:
2条回答
  • 2020-12-31 01:38

    As per the cron format

    <Minute> <Hour> <Day_of_the_Month> <Month_of_the_Year> <Day_of_the_Week> <Year>
    
    * * * * * *
    | | | | | | 
    | | | | | +-- Year              (range: 1900-3000)
    | | | | +---- Day of the Week   (range: 1-7, 1 standing for Monday)
    | | | +------ Month of the Year (range: 1-12)
    | | +-------- Day of the Month  (range: 1-31)
    | +---------- Hour              (range: 0-23)
    +------------ Minute            (range: 0-59)
    

    the solution should be

    * 11,16,21 * * * *
    
    0 讨论(0)
  • 2020-12-31 01:42

    Right solution:

    * 11,16,21 * * *
    

    Because if you use previous solution:

    0-59 11-12,16-17,21-22 * * * *
    

    Job will start at 12:40 or 17:59. It is not in range from 11AM to 12AM, 4PM to 5PM and 9PM to 10PM.

    UPDATE:

    Traditional (inherited from Unix) cron format consists of five fields separated by white spaces:

    *    *    *    *    *  command to be executed
    ┬    ┬    ┬    ┬    ┬
    │    │    │    │    │
    │    │    │    │    │
    │    │    │    │    └───── day of week (0 - 6) (0 is Sunday, or use names)
    │    │    │    └────────── month (1 - 12)
    │    │    └─────────────── day of month (1 - 31)
    │    └──────────────────── hour (0 - 23)
    └───────────────────────── min (0 - 59)
    

    nnCron can use both traditional and "enhanced" version of cron format, which has an additional (6th) field: Year.

    0 讨论(0)
提交回复
热议问题