How to instruct cron to execute a job every second

2019-01-05 03:25发布

I would like to run a job through cron that will be executed every second Tuesday at given time of day. For every Tuesday is easy:

0 6 * * Tue

But how to make it on "every second Tuesday" (or if you prefer - every second week)? I would not like to implement any logic in the script it self, but keep the definition only in cron.

11条回答
ゆ 、 Hurt°
2楼-- · 2019-01-05 04:11

Maybe a little dumb, but one could also create two cronjobs, one for every first tuesday and one for every third.

First cronjob:

0 0 8 ? 1/1 TUE#1 *

Second cronjob:

0 0 8 ? 1/1 TUE#3 *

Not sure about the syntax here, I used http://www.cronmaker.com/ to make these.

查看更多
别忘想泡老子
3楼-- · 2019-01-05 04:12

pilcrow's answer is great. However, it results in the fortnightly.sh script running every even week (since the epoch). If you need the script to run on odd weeks, you can tweak his answer a little:

0 6 * * Tue expr \( `date +\%s` / 604800 + 1 \) \% 2 > /dev/null || /scripts/fortnightly.sh

Changing the 1 to a 0 will move it back to even weeks.

查看更多
爷、活的狠高调
4楼-- · 2019-01-05 04:13

Cron provides an 'every other' syntax "/2". Just follow the appropriate time unit field with "/2" and it will execute the cronjob 'every other time'. In your case...

0 6 * * Tue/2

The above should execute every other Tuesday.

查看更多
我只想做你的唯一
5楼-- · 2019-01-05 04:14

If you want every tuesday of second week only:

00 06 * * 2#2

查看更多
该账号已被封号
6楼-- · 2019-01-05 04:18

What about every 3rd week?

Here's my suggestion:

0 6 * * Tue expr `date +\%W` \% 3 == 0 > /dev/null || /scripts/fortnightly.sh

... or ...

0 6 * * Tue expr `date +\%W` \% 3 == 1 > /dev/null || /scripts/fortnightly.sh

... or of course ...

0 6 * * Tue expr `date +\%W` \% 3 == 2 > /dev/null || /scripts/fortnightly.sh

... depending on the week rotation.

查看更多
看我几分像从前
7楼-- · 2019-01-05 04:18

Syntax "/2" is not goes along with weekday. So my added to the smart above is simply use 1,15 on MonthDay filed.

0 6 1,15 * * /scripts/fornightly.sh > /dev/null 2>&1

查看更多
登录 后发表回答