1

I am trying to schedule a cron job where the requirement is the job has to run every day, every month and every min and every hour EXCEPT 2nd and 4th sunday of every month from 1 to 3 AM.

Is there a way we can do it ? I have tried using below, but no luck:

* 0-1,3-23 1-3,5-17,19-31 * 1-6
  • 1
    Danger danger! You have set both day-of-month and day-of-week to non-* values; these fields unlike the others will cause the command to be run when either field matches the current time. There should be a note in crontab(5) about this charming little caveat. – thrig Oct 14 '15 at 15:59
1

With cron alone such specific time exceptions are not possible to configure. Instead you should check that within the script, so that the script will not run when those exceptions are met. This could then for example look like this:

#!/bin/sh

# If it's sunday ...
if [ "$(date +%u)" = "7" ]; then
  # and it's the 2nd or ...
  if ( [ "$(date +%e)" -gt "7" ] && [ "$(date +%e)" -lt "15" ] ) || \
  # or the 4th sunday of the month ...
  ( [ "$(date +%e)" -gt "21" ] || [ [ "$(date +%e)" -lt "29" ] ); then
    # look if it's between 1 and 3 AM ...
    if [ "$(date +%k)" -ge "1" ] && [ "$(date +%k)" -le "3" ]; then
      # exit the script if all of the above have met
      exit
    fi
  fi
fi

# normal script continues here

Add this to the beginning of the script.

0

What you could do is set up a job to execute during the entire duration of the period you want to exclude:

# crontab
# MIN HOUR DAY MONTH DAYOFWEEK  COMMAND
0 1 1-7,15-21 * 7 ID=naptime /path/to/naptime.sh

naptime.sh:

#!/bin/sh
# Exit unless Sunday
if [ $(date +%u) -ne 7 ] ; then
    exit
fi
sleep 2h

Then you could set up your target job to "depend" on the naptime job:

# crontab
* * * * * FREQ=1m AFTER=naptime/1m /path/to/job.sh

From crontab(1):

   Jobs can be made to "depend" on, or wait until AFTER other jobs have successfully completed.  Consider the following crontab:

          * * * * * ID=job5 FREQ=1h AFTER=job4/30m second_command

   Here, whenever job5 is up to be run, if job4 is scheduled to run within the next 30 minutes (30m), job5 will first wait for it to successfully complete.
0

First, run your script every minute:

* * * * * /usr/local/script.sh

Then put the logic in it to make it exit immediately if it's:

  1. second Sunday or fourth Sunday, or
  2. from 1 to 3 AM

This sample script relies on ncal and awk:

#!/bin/bash
second_sunday=$(ncal -h | awk '/Su/ {print $(NF-2)}')
fourth_sunday=$(ncal -h | awk '/Su/ {print $(NF)}')
if [[ $(date +'%d') -eq $second_sunday ]] || [[ $(date +'%d') -eq $fourth_sunday ]]
then exit 0
else
    if [[ $(date +'%H') -ge 01 ]] && [[ $(date +'%H') -lt 04 ]]
        then exit 0
    fi
fi
  • @amphetamachine Thanks, I don't know what's wrong with the formatting. Couldn't get it to show right. – A.P. Oct 14 '15 at 16:13

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.