syntax for bi-weekly cron job?

wes schreiner wes at infosink.com
Fri Dec 5 13:06:19 CST 2008


Preston Hagar wrote:
> On Thu, Dec 4, 2008 at 1:21 PM, Brandon Coale
> <brandoncoale at sbcglobal.net> wrote:
>> I use RHEL4 on a DELL PowerEdge server, and I want to have a cron job run at 8:30 AM every other Monday.  I think this might be the right syntax:
>>
>> 30 8 */14 * mon command
>>
>> What I'm not sure of is if this means the Monday has to fall on the 14th of the month, or if it means every 14 days?
>>
>> Thank you,
>> Brandon Coale
>>
> 
> That will actually run the command every 14th day (on the 14th of the
> month and the 28th of the month) as well as every Monday.  From the
> crontab(5) manpage:
> 
>  ''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.
> 
> One way to try it without having to modify your script would be to do
> something like the following:
> 
> 30 8 1-7, 15-21, 29-31 * * test `date +\%a` = Mon && command

But this would run the command on a Monday the 29th and then also run 
the following Monday the 5th. It fails after months with an odd number 
of Mondays.

This works:

30  8  *  *  *  (((`date +\%s`-6*3600)/3600/24\%14-4)) || command

The -6 is a correction for local time zone, CST here, and the -4 selects 
the odd Mondays. -11 would select the even Mondays.



More information about the Linux-PowerEdge mailing list