Thursday, September 08, 2005

Cron format

anonymous wrote:
> How to make sertain task to be runned with cron on every month's first
> sunday (or months first full week day sunday)
> and how about every second sunday

I don't think cron has support for that built into it.

So, you will have to go another route. The easiest way is to run the
cron job every day for the first 7 days of the month. Only one of those
days will be a Sunday, so just check if the current day is Sunday and
exit if it isn't Sunday.

For example:

#! /bin/sh

if [ `/usr/bin/date '+%Ow' -ne 0 ]
# it's not Sunday, so exit
exit 0

# rest of cron job goes here

The second Sunday of the month can be done in a similar way. Since the
first Sunday will fall in day 1-7, and since the second Sunday is 7 days
after the first, the second Sunday will fall on day 8-14. So, run the
job every day from day 8 to day 14, and check if it's a Sunday.

I'm not sure what you mean by "months first full week day sunday".
If you mean the Sunday that falls during the first full week of the
month, then to me that is equivalent to the first Sunday of the month,
because the week begins on Sunday (in the United States, at least).

Hope that helps.

- Logan


> Actually you should be able to set the date 1-7 and the day of the week
> to 0(or sun). As cron only executes the command if all parameters
> match, this will only run when day 1-7 falls on a Sunday so no need to
> build the logic into your command.

I know you can specify by day-of-month and by day-of-week, but the
question is whether, when you do this, cron interprets it as the union
or the intersection of the two sets of days.

Looking at "man crontab", it would appear that the rule is a bit
counter-intuitive: it seems to be that it's the union, unless
one of the fields is just the asterisk ("*"), in which case it's the

I'm basing this on two examples from the from the crontab manual
page. One says that

0 0 1,15 * 1

would run the command "on the first and fifteenth of each month, as
well as on every Monday" (so the union of the two sets), and then
another example says that

0 0 * * 1

would run the command "only on Mondays" (which would be the intersection
of the two sets since in this case "*" means every day of the month).

I guess I've never looked at cron in this much depth before, but it
occurs to me that this notation is really confusing! The rules aren't
clear about when it's intersection vs. union.


Post a Comment

<< Home