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