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?
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:
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.
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.
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.
Save this bash script to /etc/opt/ranger.sh:
currenttime=$(date +%H:%M)
if [[ "$currenttime" > "23:00" ]] || [[ "$currenttime" < "06:00" ]]; then
run my Python script
fi
Then add this cronjob entry:
* * * * * /etc/opt/ranger.sh