run_and_log

[code]
#!/bin/sh

#
# Runs a file and stores output (if any) to log files. Meant to be run
# by cron jobs.
# -Los
# http://www.carlos-m.net/
#

unset PATH
DATE=/bin/date
SH=/bin/sh
CAT=/bin/cat
RM=/bin/rm
ECHO='/bin/echo'

TMPDIR=$HOME/tmp

TMPFILE=$TMPDIR/`$DATE +%Y%m%d%H%M%S`.$$
TMPLOG=$TMPFILE.log
TMPERR=$TMPFILE.err

LOGDIR=$HOME/var/log
LOGFILE=$LOGDIR/cron.log
ERRFILE=$LOGDIR/cron.err

TORUN=$@

$SH $TORUN > $TMPLOG 2> $TMPERR # only run Bourne Shell scripts

STAMP="`$DATE` -- $TORUN"

if [ -s $TMPLOG ]; then
$ECHO $STAMP >> $LOGFILE
$CAT $TMPLOG >> $LOGFILE
fi

if [ -s $TMPERR ]; then
$ECHO $STAMP >> $ERRFILE
$CAT $TMPERR >> $ERRFILE
fi

$RM -f $TMPLOG $TMPERR
[/code]

Leave a Reply