Search code examples
croncron-task

How to set a Cron job in Every two hour from 10-19


I had a question before 1 month regarding this. that was the interval of 1 hour and i got exact answer. below is the link to the old question

How to set a Cron job in Every one hour from 9:00 am to 6:00 pm ( Monday to Friday )

Thank you Stack Over Flow and the contributor Andy Holmes

Now I got a new requirement on Cron expression, the same way i need it in every 2 hour.

I have tried

0 9/2-18/2 * * 1-5

and

0 (9-18)/2 * * 1-5

But that doesn't help, Please help me


Solution

  • Use:

    0 10-18/2 * * 1-5
    

    You specify the hour range 9-18 and then /2 to mean step by 2 hours. The man page explains this pretty clearly:

    Step values can be used in conjunction with ranges. Following a range with /<number> specifies skips of the number's value through the range. For example, 0-23/2 can be used in the hours field to specify command execution every other hour (the alternative in the V7 standard is 0,2,4,6,8,10,12,14,16,18,20,22). Steps are also permitted after an asterisk, so if you want to say "every two hours", just use */2.

    If your interface doesn't allow this shorthand, you have to list them out by hand:

    0 10,12,14,16,18 * * 1-5