Cron job run every x weeks and on specific days

Eddie Rozenblat picture Eddie Rozenblat · Mar 13, 2012 · Viewed 17.2k times · Source

I want to create a cron job that runs every x weeks and on a specific weekdays. for example: run every 2 weeks on midnight every Sunday and Monday.

the cron expression is stored for every "plan" and i use ncrontab function in SQL Server 2008 to generate the dates of given cron expression.

Is there an expression for it? or even join of several expressions?

I've tried to use the following expression, but it always gives the the same days in months

0 0 1/14 * *

2012-01-01 00:00:00.000
2012-01-15 00:00:00.000
2012-01-29 00:00:00.000
2012-02-01 00:00:00.000
2012-02-15 00:00:00.000
2012-02-29 00:00:00.000

EDIT:
I was looking for a recurrence of every x days/weeks and the main problem with cron, is that it resets the recurrence to the first day of month every time. for example, if i start the recurrence on 29th for every 3 days, the next occurrence will be the 1st day of next month.

I've neglected cron for the next solution: http://www.codeproject.com/Articles/20343/Recurring-Date-Generator-with-Pattern-Coding

Answer

Alex picture Alex · Mar 13, 2012

try 0 0 * * 0/2,1/2

0 and 1 are Sunday and Monday, and /2 is 'every other'

EDIT:

After more research into cron, the above is incorrect because /2 indicates the step (every other value), so 0/2 is equivalent to 0,2,4,6, and 0/2,1/2 is equivalent to 0,1,2,3,4,5,6,7 which explains why you saw it interpreted as every single day of the week.

In your example above 1/14 means starting with 1st of the month, increment by 14, yielding 1,15,29

That being said, if this were for a *nix crontab, then you could've been more granular about the schedule by having something like 0 0 * * Sun,Mon check_if_should_run.sh && the_script_to_run.sh. Using Ncrontab, I can't think of a way to set up every-other-week scenario.