3

I want a cron expression which fires every 45 minutes.

According to the documentation, I have created this 0 0/45 * * * ? expression.

But it is fired in a pattern like 12:00, 12:45, 13:00, 13:45, 14:00.

But what I expect and want is to be fired at 12:00, 12:45, 13:30, 14:15.

What am I missing?

walen
  • 7,103
  • 2
  • 37
  • 58
Mori
  • 2,484
  • 5
  • 28
  • 45
  • Adding a link for this question to help anyone that goes here. https://stackoverflow.com/questions/14517795/is-the-following-cron-expression-means-every-45-minutes because the accepted answer is not as clear as this answer. – Kosmas Dec 09 '22 at 11:05

4 Answers4

7

Cron is not meant to solve such problems. It defines the exact date and times, when a trigger must be fired, not intervals. Use a simple schedule instead:

TriggerBuilder.Create()
  .StartAt(startDate)
  .WithSimpleSchedule(
    simpleScheduleBuilder => simpleScheduleBuilder.WithIntervalInMinutes(45))
  .Build();

Edit: It's either a simple schedule as above, or multiple cron triggers (Quartz jobs can have multiple triggers):

0 0/45 12/3 * * ?    # 12:00, 12:45, 15:00, 15:45, ...
0 30 13/3 * * ?      # 13:30, 16:30, ...
0 15 14/3 * * ?      # 14:15, 17:15, ...
Rafał Rutkowski
  • 1,419
  • 10
  • 11
  • Unfortunately I am enhancing a project which former developers have not implemented support for simple schedule. That would be too much work for me to add the support for simple schedule. – Mori Mar 16 '15 at 15:22
  • I have edited the answer. I don't see any way to do it with a single cron trigger. – Rafał Rutkowski Mar 17 '15 at 09:11
3

0 0/45 * * * ? actually means "fire every 45 minutes, starting at minute 0 of every hour".

You claim your current output is 12:00, 12:45, 13:00, 13:45, 14:30, which makes sense except for the last time of 14:30.

The job is firing at the start of the hour (12:00), fires again 45 minutes later (12:45), and then it repeats, firing the next job starting at minute 0 (13:00), followed by another trigger at 45 minutes past the hour (13:45).

I'm uncertain as to why it would suddenly start firing at 14:30, but if you change your cron input to

0 45 * * * ? I believe it will work as you would like, firing every 45 minutes.

enthe0s
  • 71
  • 5
  • 14:30 was a mistake in writing. I already fixed it. Thanks for pointing out. But your crone is also not working. It produces this pattern 11:45, 12:45, 1:45, 2:45 ... It seems there is no way around it and the only solution is using a simple schedule. – Mori Mar 20 '15 at 16:40
2

you can change the interval to */15 if you want. but this actually runs every 45 minutes:

* * * * * (( $(( $(date '+(\%M+\%H*60)')\%45 )) )) || date >> /tmp/cron_45.out 2>&1
Mark Lybarger
  • 463
  • 3
  • 9
  • This is the only answer here that actually runs a cron every 45 minutes. It may depend on a specific `date` implementation, but it still meets the objective, and could be adapted for other outputs from that command. – Twitch Captain Jan 30 '23 at 02:49
1

This is a work-around that might help you. Create a timer for every 15 minutes

0 0/15 * * * ? *

and keep a variable that acts as a counter and increments every time the timer is triggered. When the counter reaches 3, trigger your method and reset the counter to 0. Hope this helps someone who lands here in search of answer!

daBigBug
  • 343
  • 2
  • 11