Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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  *  *  *
share|improve this question

migrated from stackoverflow.com Apr 30 '11 at 5:43

This question came from our site for professional and enthusiast programmers.

    
It looks correct to me. */5 = every five minutes, 30 = at 30 minutes, 4-7 = between the hours 4 to 7. The only thing I'm not sure is whether you can use every five minutes and then specify 30 minutes as well. What kind of behavior are you getting from this setup? –  Ancide Apr 29 '11 at 10:32
    
i have just set this task this morning, so i haven't received any result, i guess i will have to come back after 4.30am my time :) –  littlechad Apr 29 '11 at 10:36
add comment

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. 
share|improve this answer
add comment

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.
share|improve this answer
    
thanks for the help and effort man –  littlechad Apr 29 '11 at 18:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.