71

How can I set a cron to run every 2 hours between 8 am and 5 pm for example? Is this possible?

luqita
  • 831
  • 1
  • 6
  • 6
  • 9
    please read the question, it's not in Google... it's not a normal every 2 hours cron, it's every 2 hours between 8 and 5... – luqita Apr 11 '12 at 17:41
  • 5
    I searched Google and it brought me here! Thanks for the question/answer it was exactly what I was looking for. – bh5k Nov 28 '12 at 16:51

2 Answers2

98

Actually, Alex's (former) solution will run every minute, every 2 hours between 8 am and 3pm.  The right solution is:

0 8-17/2 * * * /path/command

Or even:

0 8,10,12,14,16 * * * /path/command

Which of course looks less nice, but still does what requested...

DarkAjax
  • 1,196
  • 8
  • 11
  • Why is it considered to be between 8am and 3pm if it will run at 4pm? – Aleksey Nov 25 '16 at 13:04
  • @Aleksey, Alex's [solution before being edited](http://superuser.com/revisions/411407/2) ran from 8am to 3pm, which is not what the OP wants and also that's the reason I added this answer of mine and made a comment of that, if you read everything carefully you should get it... – DarkAjax Nov 26 '16 at 22:06
9
0 8-17/2 * * * your_command.sh
Alex
  • 316
  • 3
  • 10