28

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?

2 Answers 2

49

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?

5
  • 21
    what is the difference between 0/3 and */3?
    – Ammad
    Feb 24, 2016 at 21:12
  • 2
    what is the difference 0/3 and */3?
    – shareef
    Jun 30, 2017 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. Oct 24, 2018 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, 2021 at 0: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, 2021 at 22:04
19

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

0 0 0/3 * * ?
1
  • 14
    Spring cron jobs require 6 digits instead of 5
    – Anto
    Nov 25, 2015 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.