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

[OT] Cron and day of the week

0 views
Skip to first unread message

T o n g

unread,
Mar 25, 2009, 12:30:15 PM3/25/09
to
Hi,

I know we can put day of the week info as cron schedules, but how can I
define:

- first Monday of the month
- second Monday of the month
- last Monday of the month

Thanks!

--
Tong (remove underscore(s) to reply)
http://xpt.sourceforge.net/techdocs/
http://xpt.sourceforge.net/tools/


--
To UNSUBSCRIBE, email to debian-us...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org

Dave Ewart

unread,
Mar 25, 2009, 12:40:10 PM3/25/09
to
On Wednesday, 25.03.2009 at 16:25 +0000, T o n g wrote:

> I know we can put day of the week info as cron schedules, but how can I
> define:

None of these are simple cron entries. I work with similar rules as
follows:

> - first Monday of the month

Run the cron job on the 1st to 7th of the month, where cron calls a
wrapper shell script with something like this:

#!/bin/bash
#
# From 'man date'
#
# %w day of week (0..6); 0 represents Sunday

REQUIRED_DAY=1

TODAY=`date +%w`

if [ $TODAY -eq $REQUIRED_DAY ]; then
echo The actual thing you want to happen is here
fi

> - second Monday of the month

As above, but cron job runs on 8th to 14th of each month.

> - last Monday of the month

That's harder - the *last* Monday is less clear, because of varying
numbers of days in the month. e.g. Monday 23 February 2009 was the last
Monday in February 2009, but Monday 30 March 2009 will be the last
Monday of this month (and the *fifth* Monday in March).

(Of course, if by 'last' you really mean 'fourth', then you can run your
wrapper script on 21st to 28th of each month.)

Hope that helps,

(You can also reverse the logic of the cron calls if you like, by running
the job on every Monday and then using the wrapper script to determine
if its between the 1st and 7th of the month etc.)

Dave.

--
Dave Ewart
da...@ceu.ox.ac.uk
Computing Manager, Cancer Epidemiology Unit
University of Oxford / Cancer Research UK
PGP: CC70 1883 BD92 E665 B840 118B 6E94 2CFD 694D E370
Get key from http://www.ceu.ox.ac.uk/~davee/davee-ceu-ox-ac-uk.asc
N 51.7516, W 1.2152

signature.asc

Avi Greenbury

unread,
Mar 25, 2009, 12:40:18 PM3/25/09
to
T o n g wrote:
> I know we can put day of the week info as cron schedules, but how can I
> define:
>
> - first Monday of the month
> - second Monday of the month
> - last Monday of the month

The first Monday of the month will be the only Monday within the first
seven days of the month.

0 0 1-7 * 1 command

would schedule command to run at midnight on any Monday that is between
the first and seventh day of the month inclusive, which should always be
the first Monday of that month.

--
Avi Greenbury :) http://aviswebsite.co.uk

Dave Ewart

unread,
Mar 25, 2009, 12:50:13 PM3/25/09
to
On Wednesday, 25.03.2009 at 16:34 +0000, Avi Greenbury wrote:

> T o n g wrote:
>> I know we can put day of the week info as cron schedules, but how can I
>> define:
>>
>> - first Monday of the month
>> - second Monday of the month
>> - last Monday of the month
>
> The first Monday of the month will be the only Monday within the first
> seven days of the month.
>
> 0 0 1-7 * 1 command
>
> would schedule command to run at midnight on any Monday that is between
> the first and seventh day of the month inclusive, which should always be
> the first Monday of that month.

I don't think that'll work as the original poster expects.

If you specify both the day numbers *and* the day of the week in the
crontab, the command will run when *either* matches; so your suggestion
will run:

- each day on 1st to 7th of the month;

and

- each Monday (for the whole month).

i.e. for March 2009, it will run on 1st through 7th inclusive and also
on 9th, 16th, 23rd and 30th.

signature.asc

Daryl Styrk

unread,
Mar 25, 2009, 1:00:10 PM3/25/09
to
T o n g wrote:
> Hi,
>
> I know we can put day of the week info as cron schedules, but how can I
> define:
>
> - first Monday of the month
> - second Monday of the month
> - last Monday of the month
>
> Thanks!
>

This has some nice examples.

http://www.adminschoice.com/docs/crontab.htm

Tapani Tarvainen

unread,
Mar 25, 2009, 1:20:14 PM3/25/09
to
On Wed, Mar 25, 2009 at 04:25:45PM +0000, T o n g (mlist4...@yahoo.com) wrote:

> I know we can put day of the week info as cron schedules, but how can I
> define:

Cron syntax can't handle these, but with an extra test in crontab they
can be done. Assuming you want to run "myprog" at noon:

> - first Monday of the month

0 12 * * 1 [ `date +%d` -le 7 ] && myprog

or

0 12 1-7 * * [ `date +%w` = 1 ] && myprog

> - second Monday of the month

0 12 8-14 * * [ `date +%w` = 1 ] && myprog

> - last Monday of the month

0 12 * * * 1 [ "`date +%m`" != "`date --date week +%m`" ] && myprog

The last depends on Gnu date (but that's what Debian uses,
so it's appropriate here): "date --date week" gives the date
one week from now, and if the month is different, today
is the last Monday.

--
Tapani Tarvainen

Matus UHLAR - fantomas

unread,
Mar 29, 2009, 9:31:15 AM3/29/09
to
> > T o n g wrote:
> >> I know we can put day of the week info as cron schedules, but how can I
> >> define:
> >>
> >> - first Monday of the month
> >> - second Monday of the month
> >> - last Monday of the month

> On Wednesday, 25.03.2009 at 16:34 +0000, Avi Greenbury wrote:
> > The first Monday of the month will be the only Monday within the first
> > seven days of the month.
> >
> > 0 0 1-7 * 1 command

On 25.03.09 16:42, Dave Ewart wrote:
> I don't think that'll work as the original poster expects.
>
> If you specify both the day numbers *and* the day of the week in the
> crontab, the command will run when *either* matches; so your suggestion
> will run:
>
> - each day on 1st to 7th of the month;
>
> and
>
> - each Monday (for the whole month).
>
> i.e. for March 2009, it will run on 1st through 7th inclusive and also
> on 9th, 16th, 23rd and 30th.

No, I think that _all_ fields have to match an entry to be executed.
That should exactly do the job
--
Matus UHLAR - fantomas, uh...@fantomas.sk ; http://www.fantomas.sk/
Warning: I wish NOT to receive e-mail advertising to this address.
Varovanie: na tuto adresu chcem NEDOSTAVAT akukolvek reklamnu postu.
Windows found: (R)emove, (E)rase, (D)elete

Mike Bird

unread,
Mar 29, 2009, 10:10:21 AM3/29/09
to
On Sun March 29 2009 06:25:41 Matus UHLAR - fantomas wrote:
> No, I think that _all_ fields have to match an entry to be executed.

From "man 5 crontab":

Note: The day of a command’s execution can be specified by two fields —
day of month, and day of week. If both fields are restricted (i.e.,
aren’t *), the command will be run when either field matches the cur‐
rent time. For example,
‘‘30 4 1,15 * 5’’ would cause a command to be run at 4:30 am on the 1st
and 15th of each month, plus every Friday.

--Mike Bird

Matus UHLAR - fantomas

unread,
Mar 30, 2009, 12:00:21 PM3/30/09
to
> On Sun March 29 2009 06:25:41 Matus UHLAR - fantomas wrote:
> > No, I think that _all_ fields have to match an entry to be executed.
>
> >From "man 5 crontab":

On 29.03.09 07:06, Mike Bird wrote:
> Note: The day of a command’s execution can be specified by two fields —
> day of month, and day of week. If both fields are restricted (i.e.,
> aren’t *), the command will be run when either field matches the cur‐
> rent time. For example,
> ‘‘30 4 1,15 * 5’’ would cause a command to be run at 4:30 am on the 1st
> and 15th of each month, plus every Friday.

Oh! Now this is what I'd say "sucks". I missed this as I never used it (I
think).


--
Matus UHLAR - fantomas, uh...@fantomas.sk ; http://www.fantomas.sk/
Warning: I wish NOT to receive e-mail advertising to this address.
Varovanie: na tuto adresu chcem NEDOSTAVAT akukolvek reklamnu postu.

"They say when you play that M$ CD backward you can hear satanic messages."
"That's nothing. If you play it forward it will install Windows."

0 new messages