Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determine how many weeks are in the month

Status
Not open for further replies.

kporeddy

IS-IT--Management
Sep 7, 2001
49
US
I need a script in place that reboots a machine every third Friday of the month, and runs a script on the other Fridays in the month. since cron has no way of knowing how many weeks are in each month.One possible solution is to write a script that will determine how many weeks are in the month., then create marker files to determine what the current week is. For instance,

ls /tmp/weeks_since_reboot.*

weeks_since_reboot.3

will tell you that in this case, it's time to reboot the machine. Then after, the reboot, create a marker file in /tmp called weeks_since_reboot.0 until 1 week has passed, then weeks_since_reboot.1, etc.

Some months of the year have 5 Fridays, which could complicate things.

Please give me the solution for write a scrip.

Thanks in advance.

Karan
 
This won't help you in your script, I'm afraid, but I'd be wary of putting these files in the /tmp directory. If the machine reboots between times (by accident or design), your files will be zapped as /tmp is cleared on reboot. Just a pointer, I'll go back to thinking how this might be achieved.
 
Hi Ken,

Thanks for your reply, Please tell me how to write the script to reboots a machine every third Friday of the month.

Thanks a lot

Karan
 
As we know the third Friday lies between the 15th and the 21st then try this

#!/bin/ksh
if [[ `date +%a` = Mon ]]
then
if [[ `date +%d` > 14 ]]
then
if [[ `date +%d` < 22 ]]
then
#command to do your reboot here
fi
fi
fi

 
Well for starters, here a quick and dirty way of figuring out the date of the 3rd Friday in a month ...

FRIDAY=`cal|awk '$6>0 && $6<32 {lc++;if (lc==3) {print $6}}'`

Regards,

Greg.
 
Looks good, but shouldn't the reference to Mon be Fri? You could add an else clause to carry out the other processing if the Friday in question isn't the third in the month (as per your original spec.). HTH.
 
Sorry - just to clarify - my response was to trunix' post, not grega's. Cheers.
 
Hi ken, trunix, greg,

I am wodering, I got a lots of responses. I am very thankfull to all. Now I will start to write the script.

Thank you very much

Karan
 
Whoops ! Yes it should be Fri and not Mon. I was testing the script yesterday - which just happened to be a Monday
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top