Use cron to run job every other week

my colleague was asking about this:

is there a way to run a cron job biweekly, like a script five.sh
to run every *OTHER* Friday within November

its part about every other Friday we cant find any documentation.

thx
L

You have to use some logic inside of that script to check whether it was executed last week, or two weeks ago and decide on result of that test if it should execute now. Then put crontab entry to run the script every friday :).

anything more specific? :slight_smile:

If it is only for November 2010 you use the "day of the month" field choosing two from 5,12,19,26 .

:slight_smile: and what if it isnt for November only, but for any month?

I dont think it can be done with the cron command, and I am looking for someone
to back me up. I dont think the day of week or day of month fields have support
for every *other* Friday.

Mind you I've not tried this but I believe it will work:
This entry should execute your script the 1st, 3rd, and potentially 5th Friday of each month at 8:00am. I didn't see any examples of this but I'm pretty sure it would only run if it matches both the day of month field and the day of week field. Give it a try.

0 8 1-7,15-21,29-31 * 6 /your/script/here

That won't work - if it ran say on the 30th it will also run on the week of 1 - 7.

And the answer is no - just using the crontab * * * * * entries there is no reliable way.
You need to alter the command structure - the part calling your script

Assuming your system supports

date +%s

Because every second Friday is an odd number of days since Thursday, Jan 1, 1970 and
every first Friday is an even number; plus there are 86400 seconds in a day:

when you execute this every friday it will test the day and run

# "first" friday  -- and note # generally does work in crontab files as a comment
0 8 ** 6  [ $((  $(( $(date +%s ) / 86400  )) % 2 )) -eq 0 ] && /path/to/myjob
#  "second" friday
0 8 ** 6  [ $((  $(( $(date +%s ) / 86400  )) % 2 )) -eq 1 ] && /path/to/myjob

Good point Jim. I was only looking within the scope of a single month. You're right, there is no way to do this with-in cron. :rolleyes:

It is not possible to provide crontab parameters to execute a cron every other Friday. It is possible to make a process run under cron every other Friday.

You could achieve the effect with an "at" job which starts by issuing another "at" job for two weeks hence before continuing execution.
There is an example in Hp-UX "man at" for issuing an "at" job for a specific time one week from next Tuesday. Try it before relying on it.

It is quite easy to write a wrapper script to run weekly from cron which detects the age of a marker file and updates the timestamp on such a marker file. This sort of process will need seeding with a file created with "touch" on the first iteration.

1 Like