Cron expression to be executed every 45 minutes

2019-01-15 23:01发布

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?

4条回答
放荡不羁爱自由
2楼-- · 2019-01-15 23:31

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!

查看更多
倾城 Initia
3楼-- · 2019-01-15 23:35

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.

查看更多
冷血范
4楼-- · 2019-01-15 23:37

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, ...
查看更多
淡お忘
5楼-- · 2019-01-15 23:46

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
查看更多
登录 后发表回答