cron every five minutes for three hours

I would like to set up cron run at 4.30 am every five minutes for 3 hours so it would stop executing at 7.30, how to do it? would this work ??

*/5,30 4-7 * * *
2

2 Answers

You cannot specify time like you want on a single cron line

# 4.30 - 4.59 evry 5 mins
30-59/5 4 * * *
# 5.00 - 6.55 evry 5 mins
*/5 5-6 * * *
# 7.00 - 7.30 evry 5 mins
0-30/5 7 * * * 

OR add something like this to your cron

*/5 4-7 * * * [ "$(date +%H%M)" -gt 0429 -a "$(date +%H%M)" -lt 0731 ] && YourScriptHere
# $( ) = means run command inside and get the results, same as backticks `
# be careful with date, because you can also set your computers time with it.
# man date will give you list of %LETTER options to specifu
# %H = hour, %M = minutes,
# -gt = greater than, -lt lessthan -a = and, && = continue execution if previous command
# did not return error. 

I did a test on my server with the following setup:

*/5,30 12-13 * * * /root/crontest > /var/log/crontest.log

Where the script crontest looked like this:

echo "Running at:"`eval date +%Y-%m-%d-%H:%M`

Here's the output:

Running at:2011-04-29-12:20
Running at:2011-04-29-12:25
Running at:2011-04-29-12:30
Running at:2011-04-29-12:35
Running at:2011-04-29-12:40
Running at:2011-04-29-12:45
Running at:2011-04-29-12:50

So basically the setup you have will run every 5 minutes between the hours you have specified. All the 30 does is say to run it every 30 minutes, which is already does due to the 5 every minute interval. So it won't restrict the cron from running just from 0 to 30 minutes and then 5 minutes each in that interval.

You might be able to solve it by restricting it like this:

*/5,0-30 12-13 * * * /root/crontest > /var/log/crontest.log

Edit: This won't work either since the , means OR so it will still run every 5 minutes

If this doesn't work then you'll probably have to solve it by:

  • Handling the 5 minute interval within your script
  • Change the interval from 4:30 - 7:30 to 4:00 - 7:00.
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like