On Wed, Jul 21, 2004 at 09:38:48AM -0500, Dave Sherohman wrote:
> On Tue, Jul 20, 2004 at 05:13:46PM -0700, Mike Fedyk wrote:
>
> > I want to have another cycle running every other Friday (two weeks), but
> > cron doesn't seem to be able to handle that.
>
> `date +%U` gives you the week number in the year. If there's an easy way
> to test whether a number is even or odd in the shell, then that could be
> used to run something from cron on only even/only odd-numbered weeks,
> although there's still the possibility for anomalies at the end of the
> year (week 52 -> week 00 is two evens in a row - but if this happens
> every year, testing the evenness/oddness of year + week number would
> solve it)...
>
> OK, somewhat convoluted way to do it:
>
> [ $(echo "$(date +\%U) \% 2" | bc) == 0 ] && /usr/local/sbin/backup-script
>
> This will run /usr/local/sbin/backup-script on all even-numbered
> weeks. Change the 0 to 1 for odd weeks. Also note that the %s need
> to be backslash-escaped to prevent cron from intepreting them as
> newlines/command separators. (Only tested in bash, but, IIRC, the $()
> syntax works in csh and vanilla sh also.)
>
> Anyone have a simpler even/odd week test?
In a 'real' bourne shell $(...) is not available, but `...` is.
However this can be simplified if you are working in a
posix-compatible shell. The math operators ((...)) provide
a return code of 0 or 1 depending on the zero/non-zero value
of the result. So you could do (ignoring cron required escapes)
(( $(date +%U) % 2 )) && ...
As you point out, the test would have problems at the turn
of some years.
--
Jon H. LaBadie jon AT jgcomp DOT com
JG Computing
4455 Province Line Road (609) 252-0159
Princeton, NJ 08540-4322 (609) 683-7220 (fax)
|