Trigger the script if it is first sunday of the quarterly month

Hello All,

I have script which needs to be scheduled in crontab which needs to be run only on the first sunday of the quarterly months (March, June, September,Dec).

Can anyone please help me out on this.

Thanks,
Arun.

You can specify in crontab that the script should run on every Sunday in those four months with:

minute hour * 3,6,9,12 0 command_string

But to restrict it to the 1st Sunday in each of those months you'll have to put something in the script being executed to exit immediately if the day of the month is greater than seven:

[ $(date +%e) -gt 7 ] && exit 0

Or, you can specify in crontab that the script should be run on the 1st seven days of each of the desired months and make the script exit if it isn't run on Sunday.

1 Like

Thanks a lot Don for your reply. Much appreciated.
I have written a script based on the inputs given by you. Could you please let me know whether this will work.

Thanks a lot in Advance!!

#!/bin/sh
set -vx

Echo "Checking whether it is first sunday of the month"

Date=` date +%e `
Date_From_Calendar=` cal | awk 'NF==7 && !/^Su/{print $1;exit}' ` ---- This variable will store the first sundays date of the month.

if [[ $Date_From_Calendar-le 7 ]];
then 
echo "Triggering the Script"

sh pathname/Script.sh

else

echo "The Date is greater than 7 hence the script will not execute."
fi

Hello Arun1992,

Please use code tags for commands/inputs/codes in your posts. You could try something like as follows.

DATE=`date +%d`
DATE_VAL=`cal | awk '(NR==3 && NF==7){print $1} (NR==3 && NF<7){getline;print $1}'`
if [[ $DATE != $DATE_VAL ]]
then
       ./script.ksh  #### You could call script here or mention the commands here too.
else
       exit;
fi
1 Like

Thanks Ravinder for your code. But i am having some doubts in that.

In code you have specified as != in if statement. Let say for ex, this month is march. I have scheduled in cron like the script should run only on Quarterly months first sundays. Now i am writing a small script to be put inside the main script which will check if the sunday is the first sunday of the quarterly month or not. Lets say today is March 6, 201(first sunday)

The script will starts executing, then it will giving me the output as below.

DATE=`date +%d` ----- 6
DATE_VAL=`cal | awk '(NR==3 && NF==7){print $1} (NR==3 && NF<7){getline;print $1}'` -------- 6

if [ 6 !=6 ] -- the script wont trigger.

Whether in if statement, if i use equal to(=) instaed of not equals whether it will work?

Thanks a lot for your reply on this..

Hello Arun1992,

Sorry I got confuse and thought you don't want to run script on quarterly 1st Sunday, here is the another script which will check either month is quarterly or not and then check the 1st Sunday and run it only on that day.

MONTH=`date +%m`
DATE_VAL=`cal | awk 'NR>2 && NF==7{A[++i]=$1} NR>2 && NF<7{A[++i]=0} END{print A[1]}'`
DATE=`date +%d`
if [[ $MONTH==3 || $MONTH==6 || $MONTH==9 || $MONTH==12 ]]
then
        if [[ $DATE == $DATE_VAL ]]
        then
                echo run script
        else
                exit;
        fi
else
        exit;
fi

Though you could use crontab to run on quarterly(As per your requirements months) to run it and you could remove this month check from script too in that case.

Thanks,
R. Singh

1 Like

Thanks a lot Ravinder!! I will try this one. I hope this will work :slight_smile:

minute hour 1-7 3,6,9,12 0 command_string

Anyone would know a reason why this wouldn't do it? Since the day of the week is set as 0 (Sun) and the days of the month are 1 through 7 (for the first week), wouldn't 0 negate already any day that is not Sunday, out of those first seven days?

man crontab :

1 Like

Since minute hour 1-7 3,6,9,12 0 command_string would run every day from 1 to 7 and also every Sunday, perhaps this might do it.

minute hour * 3,6,9,12 0 [ $(/usr/bin/date +%e) -le 7 ] && /path/to/script

Yes, this would do it. But it might be a bit "tidier" (for lack of a better word) to have everything in one place (like the scripts text) than spread over several places (part of the logic in cron, part of the logic in the script. If you put [ $(/usr/bin/date +%e) -le 7 ] && into the scripts text or into the commandline of the cron command should (otherwise) make no big difference.

At least this is my personal POV on the matter and maybe only makes sense because of the acquired style i work in. Technically there is no real difference and with another way of doing things the opposite may make equal sense.

I hope this helps.

bakunin

Glad you mentioned that it was your opinion, since it is debatable. I could make the argument that since the time when it runs is related to the cron job, separating the actual work from it (the script) is more logical, in that way if I decided to run it the first Sunday and the third Sunday, I do not have to get involved with the script, but rather the facility that deals with the time. Also, if I am not who created the script and I am administering an issue in the box, related to this particular run job, I could right away see what and when it is trying to run, without having to delve into any script to find out. Now, this is my reasoning.

I'll go with Bakunin:o

If you can not instantly decipher the purpose of command in cron, that code should be in script.
In this case i would use a case condition in script.
Alternatively you could be passing argument in cron to script with desired condition ($1) which one could change in crontab directly.

This is just my opinion and has nothing to do with right and wrong.

Best regards
Peasant.