[prev in list] [next in list] [prev in thread] [next in thread] 

List:       redhat-list
Subject:    cron question
From:       Cameron Simpson cs () zip ! com ! au
Date:       1999-01-31 23:47:36
[Download RAW message or body]

On 31 Jan 1999, in message <36B36C1A.B6E06892@radix.net>
  James Michael Keller <jmkeller@radix.net> wrote:
> > I have a script to do several things for me every five minutes.  However I
> > don't care for these scripts that the output be sent to root.  However, my
> > other cron jobs I want the output of sent to root.
[...]
> > # run-parts
> > 01 * * * * root run-parts /etc/cron.hourly
> > 02 4 * * * root run-parts /etc/cron.daily
> > 22 4 * * 0 root run-parts /etc/cron.weekly
> > 42 4 1 * * root run-parts /etc/cron.monthly
> > */5 * * * * root run-parts /etc/cron.minute

Here we have a variation on that problem; we have several cron jobs and
in addition to wanting them sent to non-root people for various pieces
we also want useful subject lines ("Output of cron" is pretty uninformative)
for the various jobs.

To this end we use a script called runmaint (appended) which dispatches
the output to a specified person with a useful subject. It also
cascades, so we have a single hourly job which runs the daily, weekly,
monthly etc jobs when they are also due. This also has the benefit of
not overlapping jobs (often the daily jobs may summarise hourly outputs
and such; if it starts before that hour's job finishes problems can
arise).

Our crontab now reads:
	0 * * * * /usr/local/script/runmaint hourly
I have an identical line in my own crontab.

The root instance looks in /usr/local/etc/maint for its stuff. For
non-root it looks in ~/.maint.

There are straight forward ways to deliver to another user (just tack
the address onto the end of the cron line), and to run a subjob from
within one of the maintenance scripts say something like:

	$Runmaint nis

which is from my daily scipt, and runs the script
	$HOME/.maint/nis
delivering the email to me with "nis" in the subject.

Runmaint also sources the normal user environment setup, so you can expect a
better class of $PATH etc in your scripts.

> 	That or pipe then all to /dev/null

You mean "redirect". "Pipe" has a specific, different, meaning.

Runmaint doesn't have a manual entry yet, but the source is there for
you to read.

Cheers,
--
Cameron Simpson, DoD#743        cs@zip.com.au        http://www.zip.com.au/~cs/

#!/bin/sh
#
# Cut here and feed through /bin/sh to extract:
#	runmaint
#	execif
#	mailif
#	mailsubj
#

sed 's/^X//' > 'runmaint' <<'EOF-runmaint'
X#!/bin/sh
X#
X# Run a maintenance script.
X#	- Cameron Simpson <cs@zip.com.au> 15oct96
X#
X
XUSER=${USER-$LOGNAME}; export USER
X[ -n "$USER" ] || { echo "$0: no \$USER or \$LOGNAME, bailing out" >&2
X		    exit 1
X		  }
X
XMaintDir=/usr/local/etc/maint
Xif [ -f "$0" ]
Xthen    RunMaint=$0
Xelse    RunMaint=$MaintDir/runmaint.sh
Xfi
Xcase $USER in
X    root)	MaintScripts=$MaintDir/scripts ;;
X    *)		MaintScripts=$HOME/.maint ;;
Xesac
X
Xexport MaintDir MaintScripts RunMaint
X
Xusage="Usage: $0 [-s subj] [-x] target [addresses]
X	-s subj	Subject for mail report.
X	-date datestr Use this instead of the date command output.
X	-x	Trace execution.
X	target	frequency[.host] or an ad hoc name
X	    frequency	daily	Soon after 12am.
X			dow	After daily, each day of the week \"dow\".
X			monthly	After daily/weekly, on first of month.
X			yearly	After monthly, on 1 January.
X	    host	Stuff for a specific host."
X
X. $MaintDir/env
X[ -r "$MaintScripts/env" ] && . "$MaintScripts/env"
X
Xbadopts=
X
Xxflag=
Xsubj="runmaint@$HOST $*"
Xdatestr=
X
Xwhile :
Xdo
X    case $1 in
X	--)	shift; break ;;
X	-s)	subj=$2; shift ;;
X	-date)	datestr=$2; shift ;;
X	-x)	xflag=-x ;;
X	-?*)	echo "$0: unrecognised option: $1" >&2
X		badopts=1
X		;;
X	*)	break ;;
X    esac
X    shift
Xdone
X
Xif [ $# = 0 ]; then echo "$0: missing target" >&2; badopts=1
X	       else target=$1; shift
Xfi
X
Xif [ $# = 0 ]; then addresses=$USER
X	       else addresses=$*
Xfi
X
X# note starting time
Xif [ -n "$datestr" ]
Xthen  set x $datestr
Xelse  set x `date|tr '[A-Z]:' '[a-z] '`
Xfi
Xif [ $# = 9 ]
Xthen
X      wday=$2 mon=$3 mday=$4 hh=$5 mm=$6 ss=$7 tz=$8 year=$9
X      shift; datestr=$*
X      export wday mon mday hh mm ss tz year datestr
Xelse
X      echo "$0: bad date arguments: $*" >&2
X      badopts=1
Xfi
X
X[ $badopts ] && { echo "$usage" >&2; exit 2; }
X
Xxit=0
X
Xcase $target in
X    /*)	script=$target ;;
X    *)	script=$MaintScripts/$target ;;
Xesac
X
X# run the script if present
Xif [ -r "$script" ]
Xthen
X    sh "$script" 2>&1 | mailif -s "$subj" $addresses
Xfi
X
Xif [ -r "$script.$HOST" ]
Xthen
X    $RunMaint -date "$datestr" $xflag -s "$subj - perhost($HOST)" $target.$HOST \
$addresses \ X    || xit=$?
Xfi
X
Xif [ -r "$script.$HOST@$SYSTEMID" ]
Xthen
X    $RunMaint -date "$datestr" $xflag -s "$subj - perhost($HOST@$SYSTEMID)" \
$target.$HOST@$SYSTEMID $addresses \ X    || xit=$?
Xfi
X
X# run the implied scripts
Xcase $target in
X    hourly)	if [ $hh -eq 0 ]
X		then
X		    $RunMaint -date "$datestr" $xflag -s "$subj" daily $addresses \
X		    || xit=$?
X		fi
X		;;
X    daily)	$RunMaint -date "$datestr" $xflag -s "$subj - $wday" $wday $addresses \
X		|| xit=$?
X		if [ $wday = mon ]
X		then
X		    $RunMaint -date "$datestr" $xflag -s "$subj" weekly $addresses \
X		    || xit=$?
X		fi
X		if [ $mday -eq 1 ]
X		then
X		    $RunMaint -date "$datestr" $xflag -s "$subj - monthly" monthly $addresses \
X		    || xit=$?
X		fi
X		;;
X    monthly)	if [ $mon = jan ]
X		then
X		    $RunMaint -date "$datestr" $xflag -s "$subj - yearly" yearly $addresses \
X		    || xit=$?
X		fi
X		;;
Xesac
X
Xexit $xit
EOF-runmaint

sed 's/^X//' > 'execif' <<'EOF-execif'
X#!/bin/sh
X#
X# Exec command only if input is not empty.
X#	- Cameron Simpson <cs@zip.com.au> 23jun98
X#
X
Xcmd=$0
Xusage="Usage: $cmd command [args...]"
X
X[ $# = 0 ] && { echo "$usage" >&2; exit 2; }
X
Xtrap 'rm -f "$tmp"; exit 1' 1 15
X
Xtmp=/tmp/execif.$$
Xsed -n -e '/^[ 	]*$/d
X	   :again
X	   p
X	   n
X	   b again' >"$tmp"
X
Xif [ -s "$tmp" ]
Xthen
X    "$@" <"$tmp"
X    xit=$?
Xelse
X    xit=0
Xfi
X
Xrm "$tmp"
X
Xexit $xit
EOF-execif

sed 's/^X//' > 'mailif' <<'EOF-mailif'
X#!/bin/sh
X#
X# Send mail only if input is not empty.
X#	- Cameron Simpson <cs@zip.com.au>
X#
X# Usage: mailif [-s subject] addresses...
X#
X# Break into execif and mailsubj. - cameron, 23jun98
X#
X
XPATH=$PATH:/usr/local/script:/usr/local/bin
Xexport PATH
Xexecif mailsubj ${1+"$@"}
EOF-mailif

sed 's/^X//' > 'mailsubj' <<'EOF-mailsubj'
X#!/bin/sh
X#
X# Send mail with subject line.
X#	- Cameron Simpson <cs@zip.com.au> 23jun98
X#
X
Xcmd=$0
Xusage="Usage: $cmd [-s subject] addresses..."
X
Xsubject=
Xcase $1 in -s)	subject="Subject: $2"; shift; shift ;; esac
X
X[ $# = 0 ] && { echo "$usage" >&2; exit 2; }
X
Xthem=$1; shift
Xwhile :
Xdo
X    case $# in 0) break ;; esac
X    them="$them, $1"
X    shift
Xdone
X
X( echo "To: $them"
X  [ -n "$subject" ] && echo "$subject"
X  echo
X  exec cat
X) | /usr/lib/sendmail -oi -t
EOF-mailsubj
exit 0


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic