I have the below script to help with disk space cleanup that finds logs older than a specified number of days (say 10 days). I need it to grab "active" logs as well. Problem is an "active log" will not get archived unless I put in 0 days which I don't want to do, I need to leave the past 10 days, but I need to archive active logs that tend to get large. Like apache logs that do not rotate until a restart of the app. Any ideas? Thanks.
#!/usr/bin/ksh
export PATH=$PATH:/usr/contrib/bin
export LogDir=$1
export Days=$2
date=$(date '+%Y.%m.%d-%H:%M:%S')
# Log Directory
#LogDir=/home/p029052/tools/logs
ARCHIVE_DIR=/home/p029052/tools/logs/archive
#inuse=find $LogDir/*log* -mtime +2 -exec /usr/sbin/fuser -u {} \;
#loglist='find *log* -mtime +2;'
if (( $# < 2 ))
then
echo "Usage log.rotate.sh DIRECTORYPATH DAYS (ie log.rotate.sh /opt/esisupt/logs/hds/ar/logs 10)"
exit 1
fi
for i in `find ${LogDir}/*.log* ${LogDir}/*.out* -mtime +${Days}`;do
num=`/usr/sbin/fuser -u ${i} 2>/dev/null | awk '{print $1}' | wc -l`
if [ ${num} -gt 0 ]
then
echo "-----COPYING, GZIP'ing and MOVING open file ${i} to ${i}.${date}-----"
cp ${i} ${i}.${date} | gzip ${i}.${date} | mv $i.${date}.gz ${ARCHIVE_DIR}
echo "-----zero out ${i}-----"
>${i}
else
echo "gzip'ing and moving ${i} to ${ARCHIVE_DIR}"
mv ${i} ${i}.${date}
gzip ${i}.${date}
mv ${i}.${date}*.gz ${ARCHIVE_DIR}
fi
done