Wednesday 17 February 2010

bash shell logrotate function

# Yet Another installation in the Yet Another script to do whatever series

# I have done logrotate in bash in at least two other flavours before. :-7

# bash shell logrotate function
function logroti () {
for f in $* ; do
echo logroti for f
if [ -e "$f" ] ; then
ROTFILES=$(find $f.[0-9] 2>/dev/null| sort -r )
for rf in $ROTFILES; do
BASE=${rf%.*}
NUM=${rf##$BASE.}
((NUM++))
echo logroti for rf=$rf BASE=$BASE NUM=$NUM new file $BASE.$NUM
mv -f $rf $BASE.$NUM
done
cp -f $f $f.0
cat /dev/null > $f
fi
done
}

# e.g. usage
logroti run_demo.log
while [ 1 ]; do
logrotate demo.log 2>&1 |tee -a run_demo.log
date |tee -a run_demo.log
echo "DEMO script start" |tee -a run_demo.log
tclsh RunDemo.tcl 2>&1 |tee demo.log
date |tee -a run_demo.log
echo "DEMO script exited" |tee -a run_demo.log
done

# the tcl script internally at top level has a catch {...} sError
# but some errors get past this! :)
# So am catching everything and can see how long things run for and what errors cause any problems

No comments: