rotatelog.sh 790 Bytes
Newer Older
O'Reilly Media, Inc.'s avatar
O'Reilly Media, Inc. committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
rotatelog.sh
#!/bin/sh
#
#  rotatelog.sh -- a script to rotate log files and
#                  compress archived files 
#
# Set behaviour
SYSLOGPID=/etc/syslog.pid    
LOGDIR=/var/log
LOG=rtrlog
DAYS=7
COMPRESS="/usr/bin/compress -f"
#                         
# Program body
[ -f $SYSLOGPID ] || echo "Syslog PID file doesn't exist"
if [ -d $LOGDIR ]; then
  cd $LOGDIR
  [ -f $LOG.1 ] && `$COMPRESS $LOG.1` && sleep 1
  while [ $DAYS -gt 1 ]
  do
        LOW=`expr $DAYS - 1`
        [ -f $LOG.$LOW.Z ] && mv $LOG.$LOW.Z $LOG.$DAYS.Z
        DAYS=$LOW
  done
[ -f $LOG ] || echo "Log file $LOG doesn't exist"
[ -f $LOG ] && mv $LOG $LOG.1
touch $LOG
chmod 644 $LOG
sleep 10
kill -HUP `cat $SYSLOGPID`
#
else
echo "Log directory $LOGDIR is not valid"
fi