27

I want my spring batch job to run every 3 hours

I used expression * * */3 * * ? this starts the job at the hour that is divisible by 3 e.g. say the server was started at 2 PM the job starts executing only at 3 PM - so far so good but the job keeps starting every second! Is it because I used * in the 1st position?

I tried 0 0 */3 * * ? but it is erroring out. What is the best way to achieve this?

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
user1705935
  • 371
  • 1
  • 3
  • 5

2 Answers2

47

The format is

second, minute, hour, day, month, weekday

so the correct cron expression should be

0 0 */3 * * *

If that doesn't work, what's the exact error message you are getting?

Christoph Leiter
  • 9,147
  • 4
  • 29
  • 37
  • 20
    what is the difference between 0/3 and */3? – Ammad Feb 24 '16 at 21:12
  • 2
    what is the difference 0/3 and */3? – shareef Jun 30 '17 at 20:39
  • 2
    * means every possible value in the field. ? means you don't care about the value. It's used when you have two fields that may contradict each other. – Oleksandr Loushkin Oct 24 '18 at 20:59
  • So I think */3 is every 3 hrs since the server started and 0/3 is every 3 hours where the hour is divisible by 3; so 3am, 6am, 9am, 12am, 3pm, 6pm, 9pm, 12pm. If anyone can confirm that would be great. – andrewps Apr 10 '21 at 00:50
  • 5
    Ok I just ran a job with: 0 0 0/6 * * * to try for every 6 hours and it did work, it seemed to run every 6th whole hour after the server was started. I bounced it at 5:50pm and the job ran at 11pm, 5am, 11am. I would take a guess that */6 would run at exactly every 6 hours after the server was started in this case, 11:50pm, 5:50am and so on. So they both work but 0/6 is the 6th whole hour and */6 is exactly every 6 hours. – andrewps Apr 10 '21 at 22:04
18

The right syntax to make the script to run every 3 hours is as below.

0 0 0/3 * * ?
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
N. Pradeep
  • 193
  • 2
  • 7