Reputation: 1637
This is my cron expression -
0 0,30 22-23,0-1 * * ?
I wanted to read a file every 30 minutes from 10pm to 1am everyday. That means it should read a file at 10pm, 10.30pm, 11pm, 11.30pm, 12am, 12.30am, 1am.
But above expression is reading the file at 1.30am also which I do not want. It should exclude 1.30am time.
Am I doing something wrong here? Could anyone suggest me the useful changes or issues in above expression? Thanks in advance!
Upvotes: 1
Views: 1105
Reputation: 1330
The issue with running past the 01 hour limit is due to the job running at :00 and :30. This limitation will require 2 separate cron jobs. One to run at the :00 minute and one to run at :30. This is needed in order to make the 00-01 hour limit prevent the job from running at 1:30AM.
1st job at :00 minute:
0 0 22-23,0-1 * * ?
//At second :00, at minute :00,
//every hour between 22pm and 23pm every hour between 00am and 01am, of every day
2nd job at :30 minute with top hour limit set to 00 instead of 01
0 30 22-23,0 * * ?
//At second :00, at minute :30, at 00am,
//and every hour between 22pm and 23pm, of every day
Here is an example of runtimes for both jobs:
1st job:
Thu Feb 28 22:00:00 UTC 2019
Thu Feb 28 23:00:00 UTC 2019
Fri Mar 01 00:00:00 UTC 2019
Fri Mar 01 01:00:00 UTC 2019
2nd job:
Thu Feb 28 22:30:00 UTC 2019
Thu Feb 28 23:30:00 UTC 2019
Fri Mar 01 00:30:00 UTC 2019
A great resource for playing around with Cron statements is the Cron formatter website, which will let you enter statements, then give you the plain English logic that the statement will follow. It also shows which sections of the statement resolve to minutes, hours, days, ect.. It is really helpful for creating new statements in my opinion.
Upvotes: 2
Reputation: 1023
Your current cron expression should fire @ 1:30am because it includes the hour 1, and the minutes are 0,30.
You could accomplish this with two cron expressions:
https://stackoverflow.com/a/21274226/10441990
Or you could use an expression filter:
https://stackoverflow.com/a/54692465/10441990
Upvotes: 0