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 have a cron job which runs every five minutes all day long.

I want to stop this cron job running on Sundays between 4pm and 5pm - it should start again after this time.

How can I script this?

share|improve this question

3 Answers 3

You may not need much if your cron job runs a script and you can make it check for the absence of a process set up to run between 4PM and 5PM on Sundays

For example, I use a cron job every 15 minutes which invokes rsync but only if rsync is not already running.

It uses the pidof command to check if a particular process id is running:

PID=$(pidof rsync)

if [ -z $PID ]
then 
    rsync -avz ....
fi

You will need to work out the details for your particular situation. I present it now as an idea to think about.

share|improve this answer

Stick this at the top of your cron job script (modified as you need)...

if [[ $(date +%u) -eq 7 && $(date +%H) -eq 16 ]]; then
  echo "I don't run at this time"
  exit 1
fi
echo "Something to do at other times here..."

The first instance of "date" returns the day of week (Sunday = 7), the second returns the hour (16 = 4.00pm - 4.59pm). If the script finds that it's currently day 7 and hour 16, it will exit.

share|improve this answer
1  
You can further condense the check into a single date command if you want, if [ $( date +'%u:%H' ) = "7:16" ]; –  Darren Hall Jul 7 '11 at 15:30
    
+1 Nice tweak Darren –  Linker3000 Jul 7 '11 at 15:54

What about:

*/5 * * * * job.sh > /dev/null

00 16 * * 0 touch /tmp/stopJob.lck
00 17 * * 0 rm -f /tmp/stopJob.lck

In job.sh just quit if the file exists:

if [ -f /tmp/stopJob.lck ] 
then 
  exit
fi
share|improve this answer
    
+1, nice little hack –  maxmackie Jul 7 '11 at 13:02
3  
There's just a slight risk that if the rm command isn't run for some reason (server restarted just before 1700, ntp time correction etc..), the main script will be skipped for a week. Likewise, if the touch is skipped, the main script will run when it shouldn't –  Linker3000 Jul 7 '11 at 14:13
    
Yes, that is true. For this Linker3000s approach is more stable. My solution is more self explaining by looking in the crontab. –  DaSteph Jul 7 '11 at 17:22

Your Answer

 
discard

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

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