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

cron job to run every first sunday of the month

2,109 views
Skip to first unread message

Chito Sto. Domingo

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
hi guys,

what's the cron parameters to run a job every first sunday of each month, to
run at 8am.

--
chito

James A. Williams

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
cron can't do this out of the box, you'll have to write a script and let cron
run the script. cron doesn't know how to tell what is the first Sunday of each
month.

Peter Koch

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
Hi!

"James A. Williams" wrote:
> cron can't do this out of the box, you'll have to write a script and let cron
> run the script. cron doesn't know how to tell what is the first Sunday of each
> month.

Nonsense. It can:

0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month_08am.sh >/dev/null
2>&1

You'd only need a shell script if you rely on moon phase or such...

Tschuess

Peter

Doug Freyburger

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
Peter Koch wrote:
>
> > cron can't do this out of the box
>
> Nonsense. It can:
>
> 0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month ...

Read the man page for crontab very carefully again.
From the Solaris one:

"Note that the
specification of days may be made by two fields (day of the
month and day of the week). Both are adhered to if speci-
fied as a list of elements. See EXAMPLES."

All of the other fields are ANDed together, but the two day
fields, day-of-week and day-of-month are ORed instead. This
is a pain in the butt, and not obvious without a very close
reading of the man pages, and even closer examination of the
example from the man page.

You need to run on days 1-7 and check for Sunday within the
script.


Paul Black

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
Doug Freyburger <dfre...@colltech.com> wrote:
>
> Peter Koch wrote:
> >
> > > cron can't do this out of the box
> >
> > Nonsense. It can:
> >
> > 0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month ...
>
> Read the man page for crontab very carefully again.
> From the Solaris one:

You should read a bit more of that man page:
| EXAMPLES
[snip]
| 0 0 * * 1
|
| would run a command only on Mondays.

Paul

Chris Mattern

unread,
May 10, 2000, 3:00:00 AM5/10/00
to
Congratulations, you have just repeated one of the most commonly
given wrong answers on the comp.unix hierarchy. Unless you have
a very unusual cron, your example will run every Sunday *and* on
the first seven days on month. Yes, this is amazingly broken.
Yes, it is the standard and how cron is *supposed* to work.

--
Chris Mattern
"Peter Koch" <ko...@pz.pirmasens.de> wrote in message news:39197D73...@pz.pirmasens.de...


> Hi!
>
> "James A. Williams" wrote:
> > cron can't do this out of the box, you'll have to write a script and let cron
> > run the script. cron doesn't know how to tell what is the first Sunday of each
> > month.
>

> Nonsense. It can:
>

Urban A. Haas

unread,
May 10, 2000, 3:00:00 AM5/10/00
to

Paul Black wrote:

> Doug Freyburger <dfre...@colltech.com> wrote:


> >
> > Peter Koch wrote:
> > >
> > > > cron can't do this out of the box
> > >

> > > Nonsense. It can:
> > >


> > > 0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month ...
> >
> > Read the man page for crontab very carefully again.
> > From the Solaris one:
>
> You should read a bit more of that man page:
> | EXAMPLES
> [snip]
> | 0 0 * * 1
> |
> | would run a command only on Mondays.
>
> Paul

Great, now make it ONLY run on the first Monday of the month. I did it
the opposite. I did what Paul was showing and checked for the day being
between 1 and 7 -- but I did it in the executable part of the crontab
entry.

--
Urban A. Haas
CEO - Urban Technology, Inc.
Minneapolis, MN USA
E-mail: uh...@urbantechnology.com (mailto:uh...@urbantechnology.com)
Phone: (952) 595-8810 Fax: (952) 595-8710

This e-mail was composed of 100% recycled bits.

Rev. Don Kool

unread,
May 10, 2000, 3:00:00 AM5/10/00
to

Peter Koch wrote:
>
> Hi!
>
> "James A. Williams" wrote:
> > cron can't do this out of the box, you'll have to write a script and let cron
> > run the script. cron doesn't know how to tell what is the first Sunday of each
> > month.
>

> Nonsense. It can:
>

> 0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month_08am.sh >/dev/null
> 2>&1
>
> You'd only need a shell script if you rely on moon phase or such...

LOL! I love these answers from such 'learned colleagues'.

Pete, what you propose will run on Sunday in addition to the 1st
thru 7th of every month (at 8AM). UNIX systems have these things
called "man pages". You might want to check them out sometime.

Hope this helps,
Don


--
********************** You a bounty hunter?
* Rev. Don McDonald * Man's gotta earn a living.
* Baltimore, MD * Dying ain't much of a living, boy.
********************** "Outlaw Josey Wales"
http://members.home.net/oldno7

Marvin

unread,
May 11, 2000, 3:00:00 AM5/11/00
to
James A. Williams wrote:
>
> cron can't do this out of the box, you'll have to write a script and let cron
> run the script. cron doesn't know how to tell what is the first Sunday of each
> month.
>
> "Chito Sto. Domingo" wrote:
> >
> > hi guys,
> >
> > what's the cron parameters to run a job every first sunday of each month, to
> > run at 8am.
> >
> > --
> > chito
Since Number of 1st Sundays/Year is 12, why don't you just put 12
entries in a crontab ?

like
0 5 <d1> <m1> * <Your procedure>
0 5 <d2> <m2> * <Your procedure>
...
0 5 <d12> <m12> * <Your procedure>


or put only one
0 5 * * 0 <Your procedure>
and make your procedure to touch file /var/somewhere/<month>.month
which will be the indicator, that procedure already has been ran once
and in the very begining of the procedure, make some checkings like.

if exist /var/somewhere/<current month>.month
exit
endif
!here we check for previous month file (we don't want to have trashes on
a disk)
if exist /var/somewhere/*.month file
rm /var/somewhere/*.month
endif
touch /var/somewhere/<current month>.month

Leslie Mikesell

unread,
May 11, 2000, 3:00:00 AM5/11/00
to
In article <391A3A...@mobitel.si>,

Or run it
0 8 * * 0 and start the script with:

if [ `date +%d` -gt 7 ]
then exit
fi
...

Les Mikesell
l...@mcs.com

Paul Black

unread,
May 11, 2000, 3:00:00 AM5/11/00
to
"Urban A. Haas" <uh...@urbantechnology.com> wrote:
>
> Paul Black wrote:
>
> > Doug Freyburger <dfre...@colltech.com> wrote:
> > >
> > > Peter Koch wrote:
> > > >
> > > > > cron can't do this out of the box
> > > >
> > > > Nonsense. It can:
> > > >

> > > > 0 8 1-7 * 0 /etc/cron.d/first_sunday_of_the_month ...
> > >
> > > Read the man page for crontab very carefully again.
> > > From the Solaris one:
> >
> > You should read a bit more of that man page:
> > | EXAMPLES
> > [snip]
> > | 0 0 * * 1
> > |
> > | would run a command only on Mondays.
> >
> > Paul
>
> Great, now make it ONLY run on the first Monday of the month. I did it
> the opposite. I did what Paul was showing and checked for the day being
> between 1 and 7 -- but I did it in the executable part of the crontab
> entry.

Errrm, errm, ooops. Must read what the problem is a bit more
carefully next time ...


> This e-mail was composed of 100% recycled bits.

I don't think you should have recylcled mine.

Paul

Peter Koch

unread,
May 11, 2000, 3:00:00 AM5/11/00
to
Hi!

"Rev. Don Kool" wrote:
> Pete, what you propose will run on Sunday in addition to the 1st
> thru 7th of every month (at 8AM). UNIX systems have these things
> called "man pages". You might want to check them out sometime.

Sure, i do read man pages, but english is not my mother language.
So i simply didn't realize what this meant:

Note that the specification of days may be made by two fields
(day of the month and day of the week). Both are adhered to

if specified as a list of elements. See EXAMPLES.

And i should not skip the examples, cause they are often the best
part of the man pages:

3. As an example of specifying the two types of days:
0 0 1,15 * 1
would run a command on the first and fifteenth of each
month, as well as on every Monday.

> LOL! I love these answers from such 'learned colleagues'.

I can understand why you enjoy my mistake that much. You're a
"real bastard" (TM) and i think you enjoy to hear that too ;-)

Tschuess

Peter

P.S.: Heck, i've learned something today without breaking
anything! Isn't that good news?!?

Rev. Don Kool

unread,
May 11, 2000, 3:00:00 AM5/11/00
to

Peter Koch wrote:
>
> Hi!
>
> "Rev. Don Kool" wrote:
> > Pete, what you propose will run on Sunday in addition to the 1st
> > thru 7th of every month (at 8AM). UNIX systems have these things
> > called "man pages". You might want to check them out sometime.
>
> Sure, i do read man pages, but english is not my mother language.
> So i simply didn't realize what this meant:
>
> Note that the specification of days may be made by two fields
> (day of the month and day of the week). Both are adhered to
> if specified as a list of elements. See EXAMPLES.
>
> And i should not skip the examples, cause they are often the best
> part of the man pages:
>
> 3. As an example of specifying the two types of days:
> 0 0 1,15 * 1
> would run a command on the first and fifteenth of each
> month, as well as on every Monday.
>
> > LOL! I love these answers from such 'learned colleagues'.
>
> I can understand why you enjoy my mistake that much. You're a
> "real bastard" (TM) and i think you enjoy to hear that too ;-)

Being the "BOTF" is a revered USENET tradition.

Rev. Don Kool

unread,
May 11, 2000, 3:00:00 AM5/11/00
to

Marvin wrote:
> James A. Williams wrote:

> > cron can't do this out of the box, you'll have to write a script and let cron
> > run the script. cron doesn't know how to tell what is the first Sunday of each
> > month.
> >
> > "Chito Sto. Domingo" wrote:
> > >
> > > hi guys,
> > >
> > > what's the cron parameters to run a job every first sunday of each month, to
> > > run at 8am.
> > >
> > > --
> > > chito

> Since Number of 1st Sundays/Year is 12, why don't you just put 12
> entries in a crontab ?

That's how users with root do it, not systems administrators.

Peter Koch

unread,
May 12, 2000, 3:00:00 AM5/12/00
to
Hi!

Marvin <ales.r...@mobitel.si> wrote:
> Since Number of 1st Sundays/Year is 12, why don't you just put 12
> entries in a crontab ?
>

> like
> 0 5 <d1> <m1> * <Your procedure>
> 0 5 <d2> <m2> * <Your procedure>
> ...
> 0 5 <d12> <m12> * <Your procedure>
>
>
> or put only one
> 0 5 * * 0 <Your procedure>
> and make your procedure to touch file /var/somewhere/<month>.month
> which will be the indicator, that procedure already has been ran once
> and in the very begining of the procedure, make some checkings like.
>
> if exist /var/somewhere/<current month>.month
> exit
> endif
> !here we check for previous month file (we don't want to have trashes on
> a disk)
> if exist /var/somewhere/*.month file
> rm /var/somewhere/*.month
> endif
> touch /var/somewhere/<current month>.month

That looks much too complicated and wouldn't work the next year.
If you have such a solution in mind, you could use "at".

Leslie Mikesell wrote:
> Or run it
> 0 8 * * 0 and start the script with:
>
> if [ `date +%d` -gt 7 ]
> then exit
> fi
> ...

That's a cute solution.

A while ago someone had asked me for a solution running a
cron job at the LAST day of the month. Anyone? Don???

Tschuess

Peter

Michael Sternberg

unread,
May 12, 2000, 3:00:00 AM5/12/00
to
Peter Koch wrote:

> Leslie Mikesell wrote:
> > if [ `date +%d` -gt 7 ]
> > then exit
> > fi
> > ...
> A while ago someone had asked me for a solution running a
> cron job at the LAST day of the month. Anyone? Don???

Marry above with something like:

cal | awk '/./ {last = $NF} END {print last}'

(as recently posted in c.u.shell)

Regards,
--
Michael Sternberg | Uni-GH Paderborn
http://www.phys.uni-paderborn.de/~stern/ | FB6 Theoretische Physik
phone: +49-(0)5251-60-2329 fax: -3435 | 33098 Paderborn, Germany
"Who disturrrbs me at this time?" << Zaphod Beeblebrox IV >> <*>

Sangeeta Mishra

unread,
May 12, 2000, 3:00:00 AM5/12/00
to
Hi

You could run the following script from th 1st to the 7th of every month
at the time you want to execute yr procedure e.g. 8:30 with the
following entry in crontab

30 8 1-7 * * <filename>

and <filename> should look like

if [ `date +%a` = Sun ]
then
<script>
else exit
fi

where script is your procedure you want to run .
I think this should help :-)

In article <391C0482...@pz.pirmasens.de>,

> > if [ `date +%d` -gt 7 ]
> > then exit
> > fi
> > ...
>

> That's a cute solution.
>

> A while ago someone had asked me for a solution running a
> cron job at the LAST day of the month. Anyone? Don???
>

> Tschuess
>
> Peter
>


Sent via Deja.com http://www.deja.com/
Before you buy.

Rev. Don Kool

unread,
May 13, 2000, 3:00:00 AM5/13/00
to

I would simply have a "cron" entry that ran on the 28-31 of each
month. It would call a script that determined if it was indeed the
last day of the current month. It is the same as the original
question of running on the first Sunday of a month. The basic
advise is keep crontab entries simple and have what it calls do the
heavy lifting.

Hope this helps,

Peter Koch

unread,
May 14, 2000, 3:00:00 AM5/14/00
to
Hi!

Michael Sternberg wrote:
> cal | awk '/./ {last = $NF} END {print last}'

Works great! And it even bears a limple solution for the
"last friday of the month" problem:

cal | awk '($6>0){last=$6} END{print last}'

Tschuess

Peter

Peter Koch

unread,
May 14, 2000, 3:00:00 AM5/14/00
to
Hi!

"Rev. Don Kool" wrote:
> I would simply have a "cron" entry that ran on the 28-31 of each
> month. It would call a script that determined if it was indeed the
> last day of the current month. It is the same as the original
> question of running on the first Sunday of a month. The basic
> advise is keep crontab entries simple and have what it calls do the
> heavy lifting.

Yes, of course. This, together with the "cal | awk ..." solution of
Michael would solve te "last day of month" problem.

Tschuess

Peter

Peter Koch

unread,
May 15, 2000, 3:00:00 AM5/15/00
to
Hi!

So cron itself cannot run a job at every first sunday
of the month, it has to be done by a shell script.

Cron will call the script on the desired hour and on
a range of days. The script will exit, if the day
doesn't match.

Crontab entry:
0 8 1-7 * * /etc/cron.d/fsothm0800.sh

fsothm0800.sh looks like:

#!/bin/sh
if [ ! `date +%a` = "Sun" ]; then
exit
fi
:
:
:

A little bit more difficult is to run a cron job on
the last day of the month.

Crontab entry:
0 5 28-31 * * /etc/cron.d/ldothm0500.sh

ldothm0500.sh:

#!/bin/sh
if [ `date +%d` -ne `cal | awk '/./ {last = $NF} END {print last}' ];
then
exit
fi
:
:
:

And finally a cron job that runs on the last friday
of the month:

Crontab entry:
0 3 21-31 * * /etc/cron.d/lfothm0300.sh

lfothm0300.sh:

#!/bin/sh
if [ `date +%d` -ne `cal | awk '($6>0){last=$6} END{print last}' ];
then
exit
fi
:
:
:

Change the two occurences of $6 in the "awk" statement
to something else for other weekdays. You can trim the
output of "cal" to other weeks with "head" and "tail".

Thanks to Leslie Mikesell, Michael Sternberg, Sangeeta
Mishra and Rev. Don Kool.

Peter

Geoff Lane

unread,
May 15, 2000, 3:00:00 AM5/15/00
to
In article <391C0482...@pz.pirmasens.de>,

Peter Koch <ko...@pz.pirmasens.de> writes:
> A while ago someone had asked me for a solution running a
> cron job at the LAST day of the month. Anyone? Don???

\begin{pseudocode}
if tomorrow is 1st of month then
doit
fi
\end{pseudocode}

The simplest way to do this is with GNU date which understands "tomorrow" as
a "date"

BTW - does anyone know how to convert Unix integer seconds into YYYY/MM/DD
using only standard (no GNU or C or perl etc) shell utilities and possibly
AWK? I need to do this and cannot assume that the system it runs on has
anything other than out-the-box solaris ( version < 8).

--
/\ Geoff. Lane. /\ Manchester Computing /\ Manchester /\ M13 9PL /\ England /\

"Bother", said Pooh, as he sunk his twelfth Guinness

0 new messages