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.

cal | nawk 'FNR==3 {i=(NF<7)?1:0}FNR==(6+i){print $1}'

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

1 Like

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.

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

1 Like

How about

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.

3 Likes

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

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.

Hello netdbaind,

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

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.

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.

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

Hope this helps.

Thanks,
R. Singh

1 Like

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

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

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

Thanks Ravindera, Rudic
I tried something this to run the script on above situation and kept cron job like this but this is not working .can anyone Please help me here .or how to test this if it is not working from the cron job.

55 03 * * * [ `date +%d` -eq `cal | nawk 'NR==4 {i=(NF<7)?1:0}NR==(7+i){print $5}'` ] &&  sh /home/nz/TEST/test_cronjob.sh  >> dev/null }