How to setup cronjob 3rd sunday of every month?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to setup cronjob 3rd sunday of every month?
# 1  
Old 10-28-2014
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 for every 4th sunday of the month.

HTML Code:
cal | nawk 'FNR==3 {i=(NF<7)?1:0}FNR==(6+i){print $1}'
# 2  
Old 10-28-2014
That will give you the date, its wont stop you having to test... (Is it the XX ?...)
So 2 options:
run every sunday and test if its the third or
run on 15-21 and test if the day is sunday
This User Gave Thanks to vbe For This Post:
# 3  
Old 10-29-2014
Hello netdbaind,

I agree with vbe's solution like we can run script on each Sunday and check either it is 3rd Sunday or not.
Following code may help you to get the 3rd Sunday with explaination.

Code:
cal | awk 'NR==3 {i=(NF<7)?1:0}NR==(5+i){print $1}'

Logic behind the code is if we need to get any 3rd occurance of a day(Sunday in this case) in a month it should come in 5th line of command cal output, but in case a month's starting day is not Sunday so number of fields NF will be less than 7 then. So by condition NF<7 we are checking the same and assigning a value of variable i. Now if number of fields are less than 7 means 1st day of a month is not Sunday so we need to jump to next line which is 6th line of command cal so we are doing here NR==(5+i) and checking if condition is true and printing it.

Hope this helps.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-29-2014 at 02:57 AM.. Reason: fine tune the lines
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 10-29-2014
How about
Code:
cal | awk 'NR==3 {print 15+NF%7}'
19

And, if you run it on every Sunday, you can check if date +"%d" is between 15 and 21.
These 3 Users Gave Thanks to RudiC For This Post:
# 5  
Old 10-30-2014
Thank you so much Ravinder,Vbe,Rudic.

Ravinder explanations was very clear,just for the curiosity if assume we want to get the date for every last i.e 4th/5th Thursday/Friday of the every month then what will be the code.
I tried something this but if assuming 4th/5th Thursday .I'm using this

Code:
cal | awk 'NR==4 {i=(NF<7)?1:0}NR==(6+i){print $5}'
23

 cal | awk 'NR==5 {i=(NF<7)?1:0}NR==(6+i){print $5}'
23

I'm getting the same output.Please Help.

Last edited by netdbaind; 10-30-2014 at 05:26 AM..
# 6  
Old 10-30-2014
Hello netdbaind,

Following may help you in same.
Let's say following is cal.

Code:
cal
    October 2014
Su Mo Tu We Th Fr Sa
          1  2  3  4
 5  6  7  8  9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Then to get the 3rd Thursday we need to do as follows.
Code:
cal | awk 'NR==4 {i=(NF<7)?1:0}NR==(5+i){print $5}'
16

To get the 4th Thursday we can do as follows.
Code:
cal | awk 'NR==4 {i=(NF<7)?1:0}NR==(6+i){print $5}'
23

Hope this helps.

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 10-30-2014
OK, provided you have GNU date, use bash as a shell, and set the shell variable month to the desired month, this will be for May 2014
Code:
month=5
date -d$((-1 * ($(date +"%j - ") $(date -d"2014-$(($month + 1))-01" +"%j + 3 + %w"))))days
Do 29. Mai 11:44:56 CEST 2014

and this for November 2014
Code:
month=11
date -d$((-1 * ($(date +"%j - ") $(date -d"2014-$(($month + 1))-01" +"%j + 3 + %w"))))days
Do 27. Nov 10:45:37 CET 2014

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

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. (13 Replies)
Discussion started by: Arun1992
13 Replies

4. UNIX for Dummies Questions & Answers

How to create a cronjob for the 3rd business day of every month?

Hello, Can you please help me out in creating a cronjob that runs every 3rd business day of the month. Thanks (2 Replies)
Discussion started by: AReddy
2 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. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

7. Solaris

Cronjob doesnot exist on the machine but running every sunday

I have deleted a particular cronjob couple of months ago but the jb is still running every sunday, even though it is not present in /var/spool/cron/crontabs/root file. Please assist. (6 Replies)
Discussion started by: prash358
6 Replies

8. Shell Programming and Scripting

crontab: setup cronjob to run first wednesday of every month

Hi, How to setup cronjob to run first wednesday of every month. Is there a way? Thanks.. (9 Replies)
Discussion started by: Anjan1
9 Replies

9. 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

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