How to specify bi-weekly crontab entries by day of the week?

Frantisek Hanzlik franta at hanzlici.cz
Sun Sep 18 11:01:38 UTC 2011


suvayu ali wrote:
> Hi,
> 
> I have been trying to figure this out but so far haven't had any
> success. I want to run jobs weekly, every 4 weeks (i.e. monthly) and
> every 52 weeks (i.e. yearly) on a specific time on a Monday.
> 
> I tried using the 1/<n> notation
> 
> 00 2 * * 1/364  /usr/bin/rsnapshot -c /etc/rsnapshot.conf yearly
> 20 2 * * 1/28   /usr/bin/rsnapshot -c /etc/rsnapshot.conf monthly
> 40 2 * * 1      /usr/bin/rsnapshot -c /etc/rsnapshot.conf weekly
> 
> but trying to save tells me:
> 
> "/tmp/crontab.yGETuJ":1: bad day-of-week
> errors in crontab file, can't install.
> Do you want to retry the same edit?
> 
> However trying something like this works but then this runs on Sundays.
> 
> 00 2 * * */364  /usr/bin/rsnapshot -c /etc/rsnapshot.conf yearly
> 20 2 * * */28   /usr/bin/rsnapshot -c /etc/rsnapshot.conf monthly
> 40 2 * * 0      /usr/bin/rsnapshot -c /etc/rsnapshot.conf weekly
> 
> So my question is, how do I specify jobs to be run every 2/4/<n> weeks
> on any arbitrary day of the week? Is that possible or do I have to
> live with only Sundays or using dates instead of day of the week?
> 
> Thanks a lot.

I'm using combination cront and script which run only at specified
days of week. It looks as (sorry for my bad english):

#------------------------------ snip ---------------------------
#!/bin/bash

# It's purpose is to run (by cron) of type "first Sunday in month".
# In cron it is activate for first 7 days in month, and on cmdline has
# specified which ordinal day in month it has execute command entered
# as second parameter.
#
# typical entry in crontab:

# for run "/usr/local/sbin/cmd" every first Saturday in month at 22:33 hr:
# 33 22 1-7 * * /usr/local/sbin/run-at-day-in-mon 6 /usr/local/sbin/cmd
#
# for run daily backup "/usr/local/sbin/backup" every Mon-Fri in month at 22:33 hr.:
# (when at 1., 9., 17. a 25.-th is running complete backup, and at other days
# except Sat+Sun we want run daily (incremental) backup:
# 33 22 2-8,10-16,18-24,26-31 * * /usr/local/sbin/run-at-day-of-week 1,2,3,4,5 /usr/local/sbin/cmd
#

#DBG=Y                  # want debug messages?
JA=${0##*/}
USAGE="Syntax: $JA day-in-week[,...] cmd [params]"
LOG="/var/log/$JA.log"

test "$DBG" = "Y" && echo "JA=$JA, \$#=$#, \$1=$1, \$2=$2."
if test $# -le 1; then echo -e "Min 2 params needed!\n$USAGE"; exit 1; fi
T="$1"
CMD="$2"
shift 2
IFSP="$IFS"
IFS=","
for DW1 in $T; do
  test "$DBG" = "Y" && echo "DW1=$DW1."
  if ! DW=$(printf "%i" $DW1 2>/dev/null); then echo "$JA: day-of-week \"$DW1\" must be number!"; exit 1; fi
  if test $DW -lt 0 -o $DW -gt 7; then echo "$JA: day-of-week \"$DW1\" must be in range 0-7!"; exit 1; fi
  AD=$(date +'%u')              # actual day of week
  test $DW -eq 0 && DW=7                # as %u return val 1-7
  test "$DBG" = "Y" && echo "DW=$DW, AD=$AD, PARAMS=$@."
  if test $AD -eq $DW; then
    echo "$(date '+%Y.%m.%d %H:%M:%S') \"$CMD\" $@" >>"$LOG"
    "$CMD" "$@"
  fi
done
#------------------------------ snip ---------------------------


Franta Hanzlik


More information about the users mailing list