Wednesday, July 14, 2010

Simple Bash script to rotate log

If you have some program that doesn't handle its own log rotation, you could use the below script to rotate the log. Ideally, you would call this script in a daily crob job.


#!/bin/bash
logfile=$1
if [ ! -f $logfile ]; then
echo "log file not found $logfile"
exit 1
fi
timestamp=`date +%Y%m%d`
newlogfile=$logfile.$timestamp
cp $logfile $newlogfile
cat /dev/null > $logfile
gzip -f -9 $newlogfile
Post a Comment