Unix Technical Forum

QUESTION: Crontab syntax for "Second Sunday in a month"

This is a discussion on QUESTION: Crontab syntax for "Second Sunday in a month" within the AIX Operating System forums, part of the Unix Operating Systems category; --> Hi, all. I'm on AIX 5.2.0.2 I have been tasked with running a series of 3 scripts once a ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 08:15 AM
BD
 
Posts: n/a
Default QUESTION: Crontab syntax for "Second Sunday in a month"

Hi, all.

I'm on AIX 5.2.0.2

I have been tasked with running a series of 3 scripts once a month, on
_different_ Sundays.

So the first script will be run on the first Sunday, the second one the
second Sunday, etc.

The first thing I tried was:

00 01 1-7 * 0

And it ran this morning, which is Day 5 (thursday).

I have subsequently discovered that if the DOW and DOM are both
specified, they are "OR-ed" rather than being "AND-ed". Hence the
scheduled task running this morning.

I have read various posts about this - one fellow says: "The only way
around this is to complicate the scripts by using cron to specify one
of the selection criteria and testing for the other criterion
_in_the_script_."

This strikes me as just plain silly.

Are there no other cron-specific ways of addressing this requirement?
Does that 'OR-ing of the DOM and DOW strike anyone else as counter to
what a cron entry 'should' do?

Thanks for all input!

BD.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 08:15 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

Yep. I had the same issue. Thought that CRON would treat it as an AND
condition. We wanted a cleanup after month-end close to run on the
second Sunday morning of the month. Went ahead with entry:

00 03 8-14 * 0

Sure enough, it started running each morning from the 8th to the 14th.
Didn't wait to see if it also ran on Sundays outside that range...we
just changed the logic. We keep the date range, but check in the script
to confirm that it is on the DOW we specified as a passed parameter.

eg.

myscript.sh 0

if test ! "$1" = ""
then
if test "$(date +"%w")" ="$1"
then
# Safe to execute
else
# Not running on correct day of week
exit 0
fi
fi


Another method would be to have all your three scripts in CRON set to
run on Sundays only but commented out. Then just go in and uncomment
the one for the correct week. If you wanted to automate that, just
create a short script that runs in CRON once a week, determines which
script should be run this particular week, then uncomments the
appropriate CRON line.

Ugly, but it works.

I'd be interested if anyone else found a better way though...That's why
I'm here

Steve

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 08:15 AM
Steve Greatbanks
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"


"BD" <bobby_dread@hotmail.com> wrote in message
news:1136482216.266407.191080@g14g2000cwa.googlegr oups.com...
> Hi, all.
>
> I'm on AIX 5.2.0.2
>
> I have been tasked with running a series of 3 scripts once a month, on
> _different_ Sundays.
>
> So the first script will be run on the first Sunday, the second one the
> second Sunday, etc.
>
> The first thing I tried was:
>
> 00 01 1-7 * 0
>
> And it ran this morning, which is Day 5 (thursday).
>
> I have subsequently discovered that if the DOW and DOM are both
> specified, they are "OR-ed" rather than being "AND-ed". Hence the
> scheduled task running this morning.
>
> I have read various posts about this - one fellow says: "The only way
> around this is to complicate the scripts by using cron to specify one
> of the selection criteria and testing for the other criterion
> _in_the_script_."
>
> This strikes me as just plain silly.
>
> Are there no other cron-specific ways of addressing this requirement?
> Does that 'OR-ing of the DOM and DOW strike anyone else as counter to
> what a cron entry 'should' do?


Depends on your cron, but the short answer is that cron does not provide
this level of granularity.
It's not that hard to have your script run every Sunday and have the
checking logic in there, then
just ensure that you have a comment in your crontab to that effect.
You could use an add-on scheduler to provide better granularity, but a case
statement will
do the trick.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 08:16 AM
mikescollan@hotmail.com
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"


Steve Greatbanks wrote:
> "BD" <bobby_dread@hotmail.com> wrote in message
> news:1136482216.266407.191080@g14g2000cwa.googlegr oups.com...
> > Hi, all.
> >
> > I'm on AIX 5.2.0.2
> >
> > I have been tasked with running a series of 3 scripts once a month, on
> > _different_ Sundays.
> >
> > So the first script will be run on the first Sunday, the second one the
> > second Sunday, etc.
> >
> > The first thing I tried was:
> >
> > 00 01 1-7 * 0
> >
> > And it ran this morning, which is Day 5 (thursday).
> >
> > I have subsequently discovered that if the DOW and DOM are both
> > specified, they are "OR-ed" rather than being "AND-ed". Hence the
> > scheduled task running this morning.
> >
> > I have read various posts about this - one fellow says: "The only way
> > around this is to complicate the scripts by using cron to specify one
> > of the selection criteria and testing for the other criterion
> > _in_the_script_."
> >
> > This strikes me as just plain silly.
> >
> > Are there no other cron-specific ways of addressing this requirement?
> > Does that 'OR-ing of the DOM and DOW strike anyone else as counter to
> > what a cron entry 'should' do?

>
> Depends on your cron, but the short answer is that cron does not provide
> this level of granularity.
> It's not that hard to have your script run every Sunday and have the
> checking logic in there, then
> just ensure that you have a comment in your crontab to that effect.
> You could use an add-on scheduler to provide better granularity, but a case
> statement will
> do the trick.


Or a simple wrap-round:-

cron entry:-
00 03 * * 0 sunday_job.sh

sunday_job.sh would be something like..........
#!/bin/ksh
day_of_month=$(date "+%d")

[ $day_of_month -gt "00" ] -a [ $day_of_month -lt "08" ] &&
1st_sunday_script.sh
[ $day_of_month -gt "07" ] -a [ $day_of_month -lt "15" ] &&
2nd_sunday_script.sh
[ $day_of_month -gt "14" ] -a [ $day_of_month -lt "22" ] &&
3rd_sunday_script.sh
[ $day_of_month -gt "21" ] && echo "4th Sunday in the Month - No script
to be run"

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 08:17 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

Woot! I like that one even better Mike! ;-)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 01-05-2008, 08:21 AM
BD
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

Interesting...

I'm not by any means a shell script expert, but I'm learning slowly.
I ran this, and it evaluated true to two of the conditionals (today's
DOM is 10):

USER:server1:/tmp/tmp_dw/date> cat date1.scr
#!/bin/ksh
day_of_month=$(date "+%d")

[ $day_of_month -gt "00" ] -a [ $day_of_month -lt "08" ] &&
1st_sunday_script.sh
[ $day_of_month -gt "07" ] -a [ $day_of_month -lt "15" ] &&
2nd_sunday_script.sh
[ $day_of_month -gt "14" ] -a [ $day_of_month -lt "22" ] &&
3rd_sunday_script.sh
[ $day_of_month -gt "21" ] && echo "4th Sunday in the Month - No script
to be run"
USER:server1:/tmp/tmp_dw/date>

USER:server1:/tmp/tmp_dw/date> ./date1.scr
../date1.scr[5]: 1st_sunday_script.sh: not found.
../date1.scr[6]: 2nd_sunday_script.sh: not found.
USER:server1:/tmp/tmp_dw/date>

Am I missing something??

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-05-2008, 08:21 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

I think it did that because it meets two criteria: 10 is greater than
"00" and less than "15"

Try this one:

#!/bin/ksh
# Testing Week of Month
#
integer CURDAY=$(date +"%e")

if test ${CURDAY} -lt 8
then
echo "Sunday # 1"
elif test ${CURDAY} -lt 15
then
echo "Sunday # 2"
elif test ${CURDAY} -lt 22
then
echo "Sunday # 3"
elif test ${CURDAY} -lt 29
then
echo "Sunday # 4"
else
echo "Sunday # 5"
fi

##### END OF SCRIPT #####

You will need to adjust the ranges if you plan to run something on an
alternate day of the week, since I only listed the maximum values that
a Sunday could ever reach each month.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-05-2008, 08:22 AM
BD
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

>I think it did that because it meets two criteria:

Interesting.

Obviously, I'm a scripting noob, but looking at your first iteration, I
interpreted the "-a" to 'AND' the two conditions together (ie -gt "00"
AND -lt "08")

'10' meets neither of these - so I figure it should not have evaluated
as true in the first line.

Could "-a" be 'or-ing' the conditions? I gather that "-a" should be
AND, and "-o" should be OR, but that doesn't seem to be how it's
behaving.

Not trying to belabor anything here, I'm just wading through the
molasses of learning this stuff. If I'm coming off as dense, just
ignore me; I'm sure I'll figure it out. ;-)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-05-2008, 08:22 AM
Laurenz Albe
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

BD <bobby_dread@hotmail.com> wrote:
> Interesting...
>
> I'm not by any means a shell script expert, but I'm learning slowly.
> I ran this, and it evaluated true to two of the conditionals (today's
> DOM is 10):
>
> USER:server1:/tmp/tmp_dw/date> cat date1.scr
> #!/bin/ksh
> day_of_month=$(date "+%d")
>
> [ $day_of_month -gt "00" ] -a [ $day_of_month -lt "08" ] &&
> 1st_sunday_script.sh
> [ $day_of_month -gt "07" ] -a [ $day_of_month -lt "15" ] &&
> 2nd_sunday_script.sh
> [ $day_of_month -gt "14" ] -a [ $day_of_month -lt "22" ] &&
> 3rd_sunday_script.sh
> [ $day_of_month -gt "21" ] && echo "4th Sunday in the Month - No script
> to be run"
> USER:server1:/tmp/tmp_dw/date>
>
> USER:server1:/tmp/tmp_dw/date> ./date1.scr
> ./date1.scr[5]: 1st_sunday_script.sh: not found.
> ./date1.scr[6]: 2nd_sunday_script.sh: not found.
> USER:server1:/tmp/tmp_dw/date>
>
> Am I missing something??


I am surprised that you get no syntax error with these [ ] statements.

I would code it like this:

[ \( "$day_of_month" -gt 0 \) -a \( "$day_of_month" -lt 8 \) ] && \
1st_sunday_script.sh

I think that the \( \) are not necessary, but they don't hurt.
Does it work if you code it similar to that?

Remember that '[ expression ]' is just a shortcut for 'test expression'.

Yours,
Laurenz Albe
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-05-2008, 08:22 AM
steven_nospam at Yahoo! Canada
 
Posts: n/a
Default Re: QUESTION: Crontab syntax for "Second Sunday in a month"

Actually you are right BD...The -a should have triggered the AND
clause. My apologies but I was not concentrating on the code that was
giving the error but rather the errors you received. It is possible
that the flaw in the logic appeared because the user is trying to
perform a numeric comparison on two string variables.

That's why in the example I submitted, it uses an integer value for
comparisons. I'm no expert either, although I do consider myself to be
following pretty good programming style for the scripts I have
written...and so far they work they way I want them to ;-)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:09 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com