2

I want my cron job to run every hour, every day except Friday when I don't want it to run at all between the hours of 2am-9am (but hourly outside of this timeframe). Ideally, I'd like to have this in one line/one cron job. What I have so far is 2 lines (and I'm not 100% sure it is correct):

0 0 * * 0,1,2,3,4,6 script.sh
0 0-1,10-23 * * 5 script.sh
  • 1
    Other than one minor correction to the first line, as in 0 * * * 0,1,2,3,4,6 script.sh, your two lines of crontab entries should do the job. I am not up to speed with the newest addition to the cron configuration, but the basic cron functionality, doesn't provide an exclusion directive. 3rd party job scheduling tools can provide that functionality. Tivoli Maestro is one that comes to mind off the top of my head. I am sure there are some open source ones out there. This is a very useful page to look at: en.wikipedia.org/wiki/List_of_job_scheduler_software – MelBurslan Feb 2 '16 at 23:51
  • @MelBurslan, thanks, that works perfectly. These 3rd party tools are interesting, and I'll keep them in mind for future projects. – Pamela Feb 3 '16 at 8:19
2

The format seems to be correct (after correction applied posted in the comment above). Are there some special restrictions for having everything in a single line? In case you need to have everything in a single line, I would suggest to change the shell script to avoid Fridays 2-9am, eg

#!/bin/bash
# THIS CODE IS NOT TESTED
# skip on fridays 2-9am
# what are the non-running times?
STARTTIME=2
ENDTIME=9

# get the current day of the week
DAY=$(date +"%u") # 1-Monday, therefore 5-Friday
# and the hour
HOUR=$(date +"%H")

if [ "$DAY" -eq 5 -a "$HOUR" -ge "$STARTTIME" -a "$HOUR" -le "$ENDTIME" ]
then
   # end the execution
   exit 0
fi

Hope that helps to solve the issue!

Frank

  • Thanks! No, there aren't any special restrictions in place limiting me to having only one line, but I just thought it would be more aesthetic. – Pamela Feb 3 '16 at 8:22

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.