Cron Expression that includes from and to time

1k Views Asked by At

I have a difficulty writing a cron expression to schedule events Mon-Saturday every 15 minutes from 4:30 am to 8:30 am.

Thanks.

2

There are 2 best solutions below

2
On BEST ANSWER

It will take 3 separate Quartz cron expressions to define the times exactly as you want them.

0 30 4,5,6,7,8 ? * MON,TUE,WED,THU,FRI,SAT *
0 45 4,5,6,7 ? * MON,TUE,WED,THU,FRI,SAT *
0 0,15 5,6,7,8 ? * MON,TUE,WED,THU,FRI,SAT *

Edited to add: This Quartz cron expression gets you from 4 am to 8:45 am, just as fvu's answer does.

0 0/15 4-8 ? * MON,TUE,WED,THU,FRI,SAT *
2
On

I don't think that you can solve this problem in one step, so a usable strategy might be to first coarse-filter via crontab:

0,15,30,45 4,5,6,7,8 * * 1,2,3,4,5,6 /do-whatever

which is almost OK, it will just execute 4:00 4:15 and 8:45, so we filter these at the start of the executed script:

# Too early?  Then get out
if [ `date +%H%M` -lt 430 ] ; then
   exit 0
fi
# Too late?  Then get out
if [ `date +%H%M` -gt 830 ] ; then
   exit 0
fi
# start of the original script
....