0

I want to create a cron expression which will run the scheduler every 2.5 min of every hour. e.g. 2.5min, 5.0min, 7.5min, 10.0min etc. of every hour. I am using Spring to create the scheduler. I tried various combination but nothing worked. One of them is as below but it is not working.

@Scheduled(cron = "*/30 */2 * * * *")

Thanks in advance.

raviadhikari
  • 31
  • 1
  • 4
  • Similar question (with answers) here https://stackoverflow.com/questions/9619362/running-a-cron-every-30-seconds/9619441 – MortimerCat Dec 29 '21 at 06:41

2 Answers2

0

That should works for you

 0 0/5 0 ? * * *
 30 2/5 0 ? * * *
  1. At second :00, every 5 minutes starting at minute :00, at 00am, of every day
  2. At second :30, every 5 minutes starting at minute :02, at 00am, of every day

You are right in this case you need to schedule your task twice using expression like on example.

sylwester
  • 16,498
  • 1
  • 25
  • 33
  • Hi @sylwester - Thanks for looking into it. I tried your expression but it will run only at 12:00 AM. `Fri Sep 14 00:00:00, Fri Sep 14 00:00:30, Fri Sep 14 00:02:30, Fri Sep 14 00:04:00, Fri Sep 14 00:04:30, Fri Sep 14 00:06:00` etc. I think 12:00 AM is happening because of 0 in hour. So I changed and try to run the expr as `0/30 0/2 * ? * * * ` and got below result. `Thu Sep 13 17:14:00, Thu Sep 13 17:14:30, Thu Sep 13 17:16:00, Thu Sep 13 17:16:30, Thu Sep 13 17:18:00, Thu Sep 13 17:18:30, Thu Sep 13 17:20:00, Thu Sep 13 17:20:30` But it also didn't work. – raviadhikari Sep 13 '18 at 17:16
  • This answer appears to be using a version of cron with a seconds parameter - I am sure they exist, but I suspect the OP's version was "minute only" – MortimerCat Dec 29 '21 at 07:11
0

There is a danger of becoming fixated on the 30 seconds. My problem was that I needed to check 18000 records for updates every month ~ 1 record every 2.5 minutes. I spent too much time trying techniques to run a job at exactly 02:32:30 before I realised that accuracy was not important.

In my situation, I realised I could execute every 2 minutes, updating my full database every 25 days instead of every 31 days.

Alternatively, I could have had 2 cron jobs running every 5 minutes. First, a 2-minute gap, followed by a 3-minute gap.

02:30 02:32 02:35 02:37 02:40 02:42 02:45 02:47

My point is that when the cron job is live, it runs unseen. Obviously, everyone has their own specific problem, but before introducing complexity, consider if it is necessary. As long as the job executes, does it really matter the exact time it ran?

MortimerCat
  • 829
  • 9
  • 26