4

I am new to Spring. I am trying to run a cron job every hour and I am using the

@Scheduled(cron="0 0/60 * * * ?")

expression for this. So when does the job start? Lets say if i have deployed the application at 10:03 AM. Will the cron Job start at the next hour i.e., 11:00 AM or it starts at 10:03 AM first and then from the next consecutive hours like 11:00, 12:00 and so on..?

Actually I deployed my application yesterday and I don't see the cron job running. I am trying to figure it out why it's not running. Meanwhile I just want to clarify myself.

I have tried to follow the documentation, but I believe they didn't mention this in the documentation: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
user3750720
  • 163
  • 1
  • 13
  • I'd expect that to be 60 minutes from app start, but I'm not sure if that's guaranteed. If you want it to be at the top of each hour, change the pattern. You could get the app to tell you when it's next going to trigger, e.g. at start up, if the pattern is available via the config; see https://stackoverflow.com/a/33504624/3001761. If it's not triggering at all, do you have `@EnableScheduling` on the app configuration class? – jonrsharpe Aug 21 '17 at 15:00
  • It assumes you know how [cron is configured](https://linux.die.net/man/5/crontab). – OrangeDog Aug 21 '17 at 15:38

1 Answers1

2

It will execute at every hour, minute 0 (same as 60), second 0 (hh:00:00). So "cron="0 0 * * * ?", will also do.

The / between values defines a range, so "cron="0 30/45 * * * ?" will execute when minute is 30 and then 15 times, until minute passes 45.

If no cron job is executed at all, it seems scheduling is not started at all. Did you set the @EnableScheduling annotation in your config?

To test the availability, set it to cron = "* * * * * ?" (every second).

Stefan
  • 12,108
  • 5
  • 47
  • 66
  • Thanks for the explanation @Stefan. Yes there is some configurational issue for the cron task and i fixed it and looks everything is fine now :) – user3750720 Aug 21 '17 at 17:26