LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   General (http://www.linuxquestions.org/questions/general-10/)
-   -   Cronjob for every second Saturday of the month (http://www.linuxquestions.org/questions/general-10/cronjob-for-every-second-saturday-of-the-month-707436/)

kinetik 02-25-2009 09:56 AM

Cronjob for every second Saturday of the month
 
I was thinking of implementing a script in crontab to run a job every second Saturday of every month, or alternatively every two weeks on a Saturday.

I'm a bit stumped with this one, not sure how it would be specified?

I was thinking along the lines of:

Code:

10  7  *  *  6  command to be executed
The above would run every Saturday at 07:10 though.


Anyone have any ideas on this?

kinetik 02-26-2009 05:30 AM

OK, think I figured it out.

I'd specify the crontab to run whichever script I want to run every Saturday. The script itself though would then just do the following:

Code:

PTS=/path/to/real/script

# -------------------------------------------------------------------------------------------------------------------------------

WDIR=`cat $PTS/counter.log`

# Is it the second Saturday?

if [ $WDIR == "1" ] ;
then
  cat /dev/null > $PTS/counter.log
  exit
elif [ $WID -ne "1" ] ;
then
  echo "1" > $PTS/counter.log
  $PTS/jobthatshouldberuneverysecondsaturday.sh
  exit
fi


Haven't tested it yet, but it should work right?

colucix 02-26-2009 05:49 AM

You can just add a control statement at the beginning of the script using the date command to check if the current day is Saturday and if it is the second of the month (it will be between the 8th and 14th of the month):
Code:

#!/bin/bash
if [ $(date +%w) -eq 6 -a $(date +%e) -gt 7 -a $(date +%e) -le 14 ]
then
  <code to be executed here>
else
  <do something>
  exit
fi


kinetik 02-26-2009 05:56 AM

Quote:

Originally Posted by colucix (Post 3457944)
You can just add a control statement at the beginning of the script using the date command to check if the current day is Saturday and if it is the second of the month (it will be between the 8th and 14th of the month):
Code:

#!/bin/bash
if [ $(date +%w) -eq 6 -a $(date +%e) -gt 7 -a $(date +%e) -le 14 ]
then
  <code to be executed here>
else
  <do something>
  exit
fi


Mmm, never thought about it like that...

Thanks Colucix, I'll give it a go and see how it goes.


All times are GMT -5. The time now is 01:53 PM.