How to schedule a biweekly cronjob?

crontab(5) defines the following fields:

       field         allowed values
       -----         --------------
       minute        0-59
       hour          0-23
       day of month  1-31
       month         1-12 (or names, see below)
       day of week   0-7 (0 or 7 is Sun, or use names)

and explains:

 Step values can be used in conjunction with ranges.  Following a range
 with ``/<number>'' specifies skips of the number's value through the
 range.  For example, ``0-23/2'' can be used in the hours field to specify
 command execution every other hour (the alternative in the V7 standard is
 ``0,2,4,6,8,10,12,14,16,18,20,22'').

So, no biweekly Jobs, as far as my understanding goes. I'm quite sure there are workarounds, what are yours? Or did I miss something?


Solution 1:

You can have the thing run by cron every wednesday, then have the thing run decide if it is an even week or an odd week. for example:

#!/bin/bash
week=$(date +%U)
if [ $(($week % 2)) == 0 ]; then 
    echo even week
else 
    echo odd week
fi

Solution 2:

Many crons (you didn't specify which you're using) support ranges. So something like

0 0 1-7,15-21 * 3

Would hit the first and third wednesdays of the month.

Note: Don't use this with vixie cron (included in RedHat and SLES distros), as it makes an or between the day-of-month and day-of-week fields instead of an and.

Solution 3:

For something that needs to run every other week use this one-liner:

0 0 * * 5 [ `expr \`date +\%V\` \% 2` -eq 0 ] && echo "execute script" 

This particular script is scheduled to run on Fridays. The week to be executed on can be adjusted by using "-eq 0" or "-eq 1"

Solution 4:

If your needs aren't literally bi-weekly, you could simply run the cronjob on the 1st and 15th of the month:

15 8 1,15 * * /your/script.sh

Which runs at 8:15 a.m. on the first and fifteenth of each month regardless of the day of the week.