1

I've seen quite a few questions here related to configuring CronExpressions. But they all seem to have exact hours (no minutes specified) as their time window for running.

Is there a way to configure a CronExpression to run say, every 5 minutes of every day between 7:37AM and 9:13PM? Configuring for 7AM - 9PM is simple. But I can't seem to figure out if there is a way (maybe it's not even supported) to run on a more detailed schedule.

I have an application that allows users to specify custom schedules. Right now I'm rounding up/down to the nearest "whole hour" as I cannot seem to figure out how to do specific times.

So for instance, this works

0 0/5 2-16 ? * 1-7 //runs every 5 minutes between 2am and 4pm every day of the week

So what would the expression be if I want to run:

every 5 minutes between 2:19AM and 4:13PM?

Or is this just not possible (outside of creating multiple CronExpressions and making it really messy imo)

Any help, greatly appreciated!

1 Answers1

0

If you enter man 5 crontab you will see the following example:-

   # Run on every second Saturday of the month
   0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"

Applying this to your case:-

   0 0/5 2-16 ? * 1-7  test $(date +\%R) \> 02:19 -a $(date +\%R) \< 16:13 && YourCommand

Alternatively, if YourCommand is a script, you can incorporate the time tests into it.

Note that, since the command is invoked at multiples of 5 minutes past the hour, tests for > and >= are equivalent (likewise < and <=); otherwise, you may need to adjust the test boundaries (eg >= 02:18), since test doesn't support >= and <= in string comparisons.

AFH
  • 17,958