[ale] crontab

JK jknapka at kneuro.net
Thu Apr 23 14:02:22 EDT 2009


Pete Hardie wrote:
> On Thu, Apr 23, 2009 at 13:33, Chris Fowler <cfowler at outpostsentinel.com> wrote:
>> I want to execute a script at 6pm on the first Thursday of each month.
>> Is there a trick in crontab I can do?
>>
>> Here are the fields allowed
>>
>>              field          allowed values
>>              -----          --------------
>>              minute         0-59
>>              hour           0-23
>>              day of month   1-31
>>              month          1-12 (or names, see below)
>>              day of week    0-7 (0 or 7 is Sun, or use names)
>>
>> The only thing I can think of off the top of my head is to run it every
>> Thursday and then check to see if we are the first Thursday of the
>> month. If not, exit 0.
> 
> That looks like the only option - the only mention I found in GNU crontab was
> that day-of-month and day-of-week will BOTH be run if specified


Sorry for the additional followup, but I'm just stunned at the
dysfunctionality of this behavior. WTF was Paul Vixie thinking?
Who ever needs a job to run on all Wednesdays AND the 5th thru
10th of every month?  Boggle.

Anyway, on to a solution.

I think it's desirable to have the info about exactly when a
job runs be present in the crontab file, not in the command
being run.  This thread on the Debian user list has a good
suggestion:

http://groups.google.com/group/linux.debian.user/browse_thread/thread/5480f439d7d1a1d1

I quote the relevant part here:

> 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. 


IOW, putting "[ date test ] && command" in crontab seems superior
IMO to having the date test inside the script being run. Someday
you might want to run it on different days.

-- JK

-- 
A closed mouth gathers no feet.


More information about the Ale mailing list