Cron expression to be executed every 45 minutes

假装没事ソ 提交于 2019-11-27 16:27:29

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, ...

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

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.

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!