syntax for bi-weekly cron job?

Tino Schwarze linux-poweredge.lists at tisc.de
Thu Dec 4 23:33:44 CST 2008


On Thu, Dec 04, 2008 at 03:37:48PM -0600, Paul M. Dyer wrote:
> The syntax is:
> 
> # Minute  Hour  Day   Month  Weekday  Command
> # 0-59    0-23  1-31  1-12   0-6
> 
> To run every Monday would be:
> 
> * * * * 1 commands   (the weekday starts from Sunday (0))
> 
> To do every other Monday, you would have to do something in the bash script to decide if you should continue:

I've got a shorter version:
declare -i day="$(date +%-d)"
declare -i weekno="$(day / 7)"

[ $weekno -eq 0 -o $weekno -eq 2 ] || exit


(Note that there might be 5 Mondays in a month.) Really getting a
bi-weekly cycle by calculation seems rather difficult (I just looked up
how many weeks a year has - it's 52 or 53, depending on the year and how
you count). The easiest way would be to use a flag file like this:

flagfile=/var/run/`basename $0`.weekly
if [ -f "$flagfile" ] ; then
    echo "Not running this week"
    rm "$flagfile"
    exit
fi
touch "$flagfile"
# do work

HTH,

Tino.

-- 
"What we nourish flourishes." - "Was wir nähren erblüht."

www.lichtkreis-chemnitz.de
www.craniosacralzentrum.de



More information about the Linux-PowerEdge mailing list