12

Today is Friday, October 3, 2014 3:58 AM

I want to schedule a cronjob like that to run it at the following dates:

  1. Saturday, October 4, 2014 8:00 AM
  2. Saturday, October 18, 2014 8:00 AM
  3. Saturday, November 1, 2014 8:00 AM ... ...

So every 2 weeks , on Saturday, at 8 o'clock.

kupa
  • 381

2 Answers2

30
0 8 * * 6 test $((10#$(date +\%W)\%2)) -eq 1 && yourCommand

date +%W: week number of year with Monday as first day of week, today week 39

10#$(date +%W): conver the date +W to decimal number and avoid shell base parsing confusion

$((39%2)): modulo operation: result is 0 (even week number) or 1 (odd week number), this week result is 1, next week 0

test 1 -eq 1: arithmetic test (equal), in this case result is boolean true

&& yourCommand: Boolean AND: run yourCommand only if result of previous command was boolean true

Note that the year can get two odd weeks: 53 (this year) and 1 (next year)

Cyrus
  • 931
5

What you've shown is "every week". Then the code is:

0 8 * * 6

Are you sure you need to run it every two weeks?

0 8 * * 6 expr `date +\%s` / 604800 \% 2 >/dev/null || yourCommand
Glueon
  • 3,774