Cron job run every x weeks and on specific days [closed]

。_饼干妹妹 提交于 2019-12-04 11:49:44

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/9687278/cron-job-run-every-x-weeks-and-on-specific-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!