Logrotation script

Here is a script that I sometimes use to implement logrotation on my Solaris servers for generic agent log files. I started to use before Solaris 9. The ‘logadm’ utility exists in Solaris 9 and 10 which has got some pretty neat features.

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
#!/bin/ksh
# logrotate -- A script to roll over log files
# Usage: logrotate /var/log/authlog [mode [revs] ]
FILE=$1
MODE=${2:-644}
DEPTH=${3:-4}
DIR=`dirname $FILE`
LOG=`basename $FILE`
DEPTH=$(($DEPTH - 1))
if [ ! -d $DIR ]; then
   echo "$DIR: Path does not exist"
   exit 255
fi
cd $DIR
while [ $DEPTH -gt 0 ]
  do
     OLD=$(($DEPTH - 1))
     if [ -f $LOG.$OLD ]; then
         mv $LOG.$OLD $LOG.$DEPTH
     fi
   DEPTH=$OLD
  done
 
if [ $DEPTH -eq 0 -a -f $LOG ]; then
    mv $LOG $LOG.0
fi
cp /dev/null $LOG
chmod $MODE $LOG

About

View all posts by

Leave a Reply

Your email address will not be published. Required fields are marked *