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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trigger the script if it is first sunday of the quarterly month
# 1  
Old 12-30-2015
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.
# 2  
Old 12-30-2015
You can specify in crontab that the script should run on every Sunday in those four months with:
Code:
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:
Code:
[ $(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.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-30-2015
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!!

Code:
#!/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

Moderator's Comments:
Mod Comment edit by bakunin: Please use CODE-tags when posting code or terminal output. Thank you.

Last edited by bakunin; 12-30-2015 at 11:03 AM..
# 4  
Old 12-30-2015
Hello Arun1992,

Please use code tags for commands/inputs/codes in your posts. You could try something like as follows.
Code:
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

This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 12-30-2015
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..

Moderator's Comments:
Mod Comment edit by bakunin: again - please use CODE- or ICODE-tags when posting code

Last edited by bakunin; 12-30-2015 at 11:06 AM..
# 6  
Old 12-30-2015
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.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 12-30-2015
Thanks a lot Ravinder!! I will try this one. I hope this will work Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

cron job on 3rd sunday of every month

Hi, Actually scheduled my test scripts on test severs as shown below. They are supposed to run on 3rd sunday of every month. Unfortunately it ran on 2nd sunday of the month (suspecting that it will run every sunday). I am sorry if I miss something. Could you please let me know if I did any... (1 Reply)
Discussion started by: System Admin 77
1 Replies

2. Shell Programming and Scripting

Run script on every last sunday of the month

Hi ALL, I have been testing this script to run for every last Sunday of the month,looks like month which have 5 sunday (july 2016 )is not passing this and failing every time. Here is what I am using, current_date=31 echo " CURRENT DAY -> $current_date" if ... (2 Replies)
Discussion started by: netdbaind
2 Replies

3. Shell Programming and Scripting

How to setup cronjob 3rd sunday of every month?

Hi All, I need to set up cronjob for every third sunday of the month. here i have seen one example for 4th sunday for every month in another post and it looks perfect.can anyone please help me to understand this and help me to get the setup for third sunday of every month.Thanks. this is... (7 Replies)
Discussion started by: netdbaind
7 Replies

4. UNIX for Dummies Questions & Answers

Script should check and run only on Sunday

Hi All, I have a script, I want to make sure the script should check whether the day is sunday, only then it should run, if it is run other days it should check and exit the script. Kindly help. Thanks in Advance !! (41 Replies)
Discussion started by: nanz143
41 Replies

5. Shell Programming and Scripting

How to get first sunday of the month?

Hi, Please can someone help me in getting first sunday date of a month. i_year=`date +%Y` ny_first_sun_nov=`cal 10 $i_year | sed '/^$/d' |head -3 |tail -1| rev | cut -c1` This works good if the first sunday has a value but not if it is blank and first sunday falls on second week. ... (17 Replies)
Discussion started by: infyanurag
17 Replies

6. Shell Programming and Scripting

Script to counting a specific word in a logfile on each day of this month, last month etc

Hello All, I am trying to come up with a shell script to count a specific word in a logfile on each day of this month, last month and the month before. I need to produce this report and email it to customer. Any ideas would be appreciated! (5 Replies)
Discussion started by: pnara2
5 Replies

7. UNIX for Dummies Questions & Answers

Run script on every second sunday

I was to schedule a script in a crontab after every 15 days specically on every 2nd Sunday. I know that i can schedule on basis of weekdays, but can it be done by skipping in between???:wall: (5 Replies)
Discussion started by: masquerer
5 Replies

8. UNIX for Dummies Questions & Answers

How to Set up a cronjob On 4th Sunday of every Month?

How to Set up a cronjob which will run On 4th Sunday of every Month at 8:00 PM :( (11 Replies)
Discussion started by: tp2115
11 Replies

9. Shell Programming and Scripting

Shell Script to Handle Quarterly Backup.

Hi I'm not to shell script and I'm trying to write a simple shell code to do the following. Shell script should be run in March, June, Sept, & Dec respectively to back up files last modified in the 1st, 2nd, 3rd, and 4th quarters. I've have the following code. #!/bin/csh set v1 = `date... (1 Reply)
Discussion started by: Magshabib@gmail
1 Replies

10. UNIX for Dummies Questions & Answers

Cron job -- to execute at every first Sunday of every month

Dear all How can I schedule the cronjob to be run sometime at every first Sunday at every month? I have the edit the cronjob every month now, thanks (2 Replies)
Discussion started by: shanemcmahon
2 Replies
Login or Register to Ask a Question