Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

cron hack for "2nd Wed of each month"??

0 views
Skip to first unread message

Dan Eble

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to reg...@worldnet.att.net

ad...@pls.post.here wrote:
>
> While the scripts work, I have not been able to figure out any
> one-line way to specify "the 2nd Wednesday of each month." For now I

date -d "1 `date +%b`" +%w | awk '{print (10-$1)%7+8}'

Returns the date of the second wednesday of the current month. You
could have cron run your script every wednesday, but have the script
abort if the current date doesn't match the date of the second
wednesday.

Anyone see a better way?

--------
Dan Eble (mailto:eb...@cis.ohio-state.edu)
(http://www.cis.ohio-state.edu/~eble)

"Behold, how great a matter a little fire kindleth!" -- James 3:5

Andrew Gierth

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to

>>>>> "Dan" == Dan Eble <eble.2@osu..edu> writes:

Dan> date -d "1 `date +%b`" +%w | awk '{print (10-$1)%7+8}'

Non-portable.

Dan> Returns the date of the second wednesday of the current month. You
Dan> could have cron run your script every wednesday, but have the script
Dan> abort if the current date doesn't match the date of the second
Dan> wednesday.

Dan> Anyone see a better way?

0 8 * * 3 d=`date +%d`; [ "$d" -gt 7 -a "$d" -lt 15 ] && /path/to/script

--
Andrew.

jose nazario

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to

Dan Eble (eble.2@osu..edu) wrote:

: ad...@pls.post.here wrote:
: >
: > While the scripts work, I have not been able to figure out any
: > one-line way to specify "the 2nd Wednesday of each month." For now I

: date -d "1 `date +%b`" +%w | awk '{print (10-$1)%7+8}'

: Returns the date of the second wednesday of the current month. You
: could have cron run your script every wednesday, but have the script
: abort if the current date doesn't match the date of the second
: wednesday.

: Anyone see a better way?

section 42.15 of UNIX Power Tools (1st ed.) has the script that should help
you. it's known as "whichweek" and can be tuned to give you any particular
day of the month (ie second wed of each month, third friday or whatever).

the script is in
ftp://ftp.uu.net/published/oreilly/power_tools/unix/upt.oct93.tar.Z

jose nazario
dept of biochemistry
case western reserve university

John Kotches

unread,
Apr 4, 1998, 3:00:00 AM4/4/98
to

My suggested solution is to kick off the cron job every wednesday,
then have the script being started check the calendar date.

ie

if [ `date +%d` -lt 8 -o `date +%d` -gt 14 ]
then
exit
fi

Probably not the elegant solution the original poster is looking for,
but the code does work.


--
************************************************************
* A goofy Unix and network administration type person *
* working for a large computer equipment manufacturer and *
* internet services provider. Opinions expressed are *
* those of the writer on good days -- On bad days they're *
* just ramblings. They AREN'T the opinion of my employer. *
* *
* jkotches@_pobox_.com, solaris dude in a mostly AIX shop. *
************************************************************

wbe

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

ad...@pls.post.here writes:
> Anyone have some way to specify such a quantity as the 2nd Wednesday
> of the month from within cron? Any ideas of possible hacks?

How about doing part with cron and part with date and test:

0 8 7-13 * * test `date +%a` = Tue && send email alert
0 8 8-14 * * test `date +%a` = Wed && run reboot script

Or, make use of "at"s ability to schedule jobs based on the day of the week
(note: the 'cron' and 'at' times relationship is significant):

0 6 7 * * at 8am Tue email-alert-script; at 4am Wed run-reboot-script
-WBE

Rich Kus

unread,
Apr 5, 1998, 4:00:00 AM4/5/98
to

In article <ydvhsp9...@ubeblock.psr.com>,

Winston Edmond <wbe @[ubeblock ]psr.com> wrote:
>ad...@pls.post.here writes:
>> Anyone have some way to specify such a quantity as the 2nd Wednesday
>> of the month from within cron? Any ideas of possible hacks?
>
>How about doing part with cron and part with date and test:
>
> 0 8 7-13 * * test `date +%a` = Tue && send email alert
> 0 8 8-14 * * test `date +%a` = Wed && run reboot script

0 8 7-13 * 2 send email alert
0 8 8-14 * 3 run reboot script

How about using the day of the week field matching day of the month
field. cron script should run the script when all field match.

richk

Casper H.S. Dik - Network Security Engineer

unread,
Apr 6, 1998, 3:00:00 AM4/6/98
to

[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]

ri...@panix.com (Rich Kus) writes:

}In article <ydvhsp9...@ubeblock.psr.com>,
}Winston Edmond <wbe @[ubeblock ]psr.com> wrote:
}>ad...@pls.post.here writes:
}>> Anyone have some way to specify such a quantity as the 2nd Wednesday
}>> of the month from within cron? Any ideas of possible hacks?
}>
}>How about doing part with cron and part with date and test:
}>
}> 0 8 7-13 * * test `date +%a` = Tue && send email alert
}> 0 8 8-14 * * test `date +%a` = Wed && run reboot script

} 0 8 7-13 * 2 send email alert
} 0 8 8-14 * 3 run reboot script

} How about using the day of the week field matching day of the month
} field. cron script should run the script when all field match.

Except when it comes to the day-of-week, this is an "or" field.

(That's a bug in the standard, IMO)

Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

Ingvar Mattsson

unread,
Apr 6, 1998, 3:00:00 AM4/6/98
to

ri...@panix.com (Rich Kus) writes:

>
> In article <ydvhsp9...@ubeblock.psr.com>,
> Winston Edmond <wbe @[ubeblock ]psr.com> wrote:
> >ad...@pls.post.here writes:
> >> Anyone have some way to specify such a quantity as the 2nd Wednesday
> >> of the month from within cron? Any ideas of possible hacks?
> >
> >How about doing part with cron and part with date and test:
> >
> > 0 8 7-13 * * test `date +%a` = Tue && send email alert
> > 0 8 8-14 * * test `date +%a` = Wed && run reboot script
>
> 0 8 7-13 * 2 send email alert
> 0 8 8-14 * 3 run reboot script
>
> How about using the day of the week field matching day of the month
> field. cron script should run the script when all field match.

Ayup, but many crons AND all fields, but ORs the "day of month" and
"day of week" field. So something to be run at:
8 8 13 * 5 friday13thprog # Runs every friday and every 13th
will be started every 13th and every Friday.

//Ingvar (I've been bitten by it myself, I have)
--
Sysadmin, disgruntled, unpolite.
I don't speak for my employer nor do they speak for me.
Accept this and life will be easier. ing...@idasys.se

fna...@usa.net

unread,
Apr 6, 1998, 3:00:00 AM4/6/98
to

In article <3526ECD1.1AAA674A@_pobox_.com>,
John Kotches <jkotches@_pobox_.com> wrote:
>
>
[snip]

> My suggested solution is to kick off the cron job every wednesday,
> then have the script being started check the calendar date.
>
> ie
>
> if [ `date +%d` -lt 8 -o `date +%d` -gt 14 ]
> then
> exit
> fi
>
> Probably not the elegant solution the original poster is looking for,
> but the code does work.

This is a very elegant solution conceptually speaking. Depends only on the
fact that a week has always seven days (very reasonable :) and the standard
definition of 'date'. It could be coded shorter:

[ `date +%d` -lt 8 -o `date +%d` -gt 14 ] && exit

My solution is not so elegant because it relies on the output of the 'cal'
command. But on the other side, it displays the day of the the 2nd
Wednesday of the current month:

d=`cal | awk -F' ' '/^[0-9 ]*$/ {print $4}' | grep -v '^ *$' | head -2 |
tail -1` [ $d -eq `date +%d` ] && exit

>
> --
> ************************************************************
> * A goofy Unix and network administration type person *
> * working for a large computer equipment manufacturer and *
> * internet services provider. Opinions expressed are *
> * those of the writer on good days -- On bad days they're *
> * just ramblings. They AREN'T the opinion of my employer. *
> * *
> * jkotches@_pobox_.com, solaris dude in a mostly AIX shop. *
> ************************************************************
>

-Fernando


-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/ Now offering spam-free web-based newsreading

Max Jester

unread,
Apr 6, 1998, 3:00:00 AM4/6/98
to

ad...@pls.post.here wrote:
[...deletia...]

> Anyone have some way to specify such a quantity as the 2nd Wednesday
> of the month from within cron? Any ideas of possible hacks?

The first quick-n-dirty method that springs to mind is to set it for
every Wednesday, then check within the script that the day-of-month
falls between 8 and 14, else exit without executing further.

HTH
Max
--
Down, not across


Jan Birk

unread,
Apr 15, 1998, 3:00:00 AM4/15/98
to

Hi

Here is a way to get the 2nd wednesday:

cal | grep -v [a-z] | cut -c10-11 | awk 'NR == 2 { print $0 }'

It will ( I think) work many years

Hope it helps

Best regards

Jan Birk

ad...@pls.post.here wrote in article
<6g5uki$3...@bgtnsc02.worldnet.att.net>...
> Hello,
>
> One of our machines needs to be rebooted, by agreement with the users,
> the 2nd Wednesday of each month. What I would like to do is put up
> some cron jobs, one that emails me and them to remind us of the reboot
> the day before and then another that does the actual shutdown.


>
> While the scripts work, I have not been able to figure out any
> one-line way to specify "the 2nd Wednesday of each month." For now I

> have hardcoded in each Tues (for the email) and Wed (for another mail
> and the script) from now until the end of the year. Needless to say
> this is quite a clutter in the cron file and relies upon a SysAdmin to
> remember to update it in December, etc.


>
> Anyone have some way to specify such a quantity as the 2nd Wednesday
> of the month from within cron? Any ideas of possible hacks?
>

> thanks,
>
> Adam
>
> post here so everyone can benefit, but to write me:
> r e g n i s a t w o r l d n e t d o t a t t d o t n e t
>

Earl Barfield

unread,
Apr 20, 1998, 3:00:00 AM4/20/98
to

"Jan Birk" <jb...@relief.dk> writes:
>>
>> One of our machines needs to be rebooted, by agreement with the users,
>> the 2nd Wednesday of each month. What I would like to do is put up
>> some cron jobs, one that emails me and them to remind us of the reboot
>> the day before and then another that does the actual shutdown.

The way we do it is to run a script from a cron job. The cron job
runs on the 7th through the 14th of the month. The very first thing
the script does is check to see if the days is wednesday.

0 6 7-14 * * /usr/local/etc/reboot_on_wednesday


#!/bin/ksh

if [ "`/bin/date +%a`" -eq "Wed" ]
then

/usr/sbin/shutdown -y -i0 -g0
fi
--
Earl Barfield -- Operations Department / Information Technology
Georgia Institute of Technology, Atlanta Georgia, 30332
Internet: Earl.B...@oit.gatech.edu ea...@prism.gatech.edu
ea...@fantasy.gatech.edu ea...@oit.gatech.edu

0 new messages