Wednesday, September 17, 2008

TRIMMING THE LOG

Various unix processes canproduce fast growing logs thatsometimes need to be trimmedinstead of deleted, for referenceor troubleshooting. And youlikely have no desire to edit thefiles. Here is a handy kshscript that will quickly trimthe log so it keeps recentinformation and lets you keep asmany lines as you think you mightneed.

I call it trimlog:

#! /bin/sh
# trimlog
filesize=`cat $1wc -l`
trim=`expr $filesize - $2`
if [ $trim -gt 0 ]
then
sed "1,$trim d" $1 > /tmp/$1
mv /tmp/$1 $1
echo $1 trimmed by $trim lines
else
echo "Invalid argument"
fi
Use it by feeding in the name of the log you want to trim followedby the number of lines you want to keep:

# trimlog oracle_listener.log 10000

Of course you can't use it in the /tmp directory.
There are probably newer and more efficient ways to do this, but this works.

No comments: