问题
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?
回答1:
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, ...
回答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
回答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.
回答4:
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!
来源:https://stackoverflow.com/questions/29074816/cron-expression-to-be-executed-every-45-minutes