4

I have a python program which I need to run every minute from 11PM (EDT) to 06AM (EDT). How can I schedule a cron job to do this?

* 23-6 * * 1-5 python my_program.py

will this work? or do I have to write 2 separate cron jobs for this?

9

Ranges that wrap around like that are ambiguous. Specify the hours as 23,0-6 instead and avoid future problems.

Cron checks every minute the contents of crontab files and if it founds coincidence of the time and the conditions it will run the script indicated on the line.

For this case these is the set of coincidences that must be met:

  • From 11 PM to 11:59 PM and from 0:00 to 6:59 AM
  • From monday to friday

So, every minute during the time that the set of coincidences are true, it will run.

Don't expect it to run outside of the range of hours and days indicated, for example on saturday.

  • It's actually going to run from 23:00 to 6:59, not 6:00. See @roaima's answer below. Sorry, but -1 until this is fixed. – Erathiel May 19 '15 at 10:16
  • Fixed. I forgot that it was every minute. – jcbermu May 19 '15 at 10:17
4

Your schedule as given is ambiguous, particularly as there are several different cron implementations available on Unix/Linux systems. I would strongly recommend that you specify two ranges to satisfy the wrap-around over midnight (but note that according to Crontab entry with hour range going over midnight the AT&T/BSD cron implementations can't handle lists of ranges).

However, perhaps more importantly, your definition will run your program every minute from 2300 (11pm) until 0659 (6.59am), which is almost an hour later than required. It will also only run while the day is Monday-Friday (1-5). If you want the script to run from 11pm Monday night, but stopping 6am Saturday morning, you will need these entries:

* 23  * * 1-5 python my_program.py    # 11pm-midnight Mon-Fri
* 0-5 * * 2-6 python my_program.py    # midnight-5.59am Tue-Sat
0 6   * * 2-6 python my_program.py    # 6am Tue-Sat

Finally, be aware that cron usually runs using the system default timezone, which may not necessarily be the timezone that you are using interactively. Some testing may be required to ensure that your 11pm-6am matches cron's idea of 11pm-6am.

  • 2
    Very good point about 6:59! – Erathiel May 19 '15 at 10:14
1

This will run your job every minute from 11:00pm to 06:59am on the days 1-5 of the week.

Here 1 ==> Monday and 5 ==> Friday.

However, cron will check that every condition is satisfied and on Friday, it will stop 12 midnight since Saturday is not included in the days. On all other days, it will work fine.

  • Thanks. So on friday it will run from friday 11PM to Saturday 06AM right? – Chiyaan Suraj May 18 '15 at 10:35
  • No. It will run on friday 11PM, and the next coincidence is 0:00 next monday. – jcbermu May 18 '15 at 10:51
  • Same comment as for @jcbermu: it's actually going to run from 23:00 to 6:59, not 6:00. See roaima's answer below. Sorry, but -1 until this is fixed. – Erathiel May 19 '15 at 10:17
  • @Erathiel thanks for pointing it out. I've fixed it now. – shivams May 19 '15 at 10:59

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.