uncategorized

Daily Log Maintenance

For systems that have lots of application logs, this shell script can be run from cron on a daily basis to maximize space utiliazation by gzipping and deleting old log files by the defined time limitations

#!/bin/sh  
#
# app/logs/current log maintenance. by: jhazelwo, 2012
#
# meant to be run daily from cron
#
#30 0 * * * /app/bin/logMaint.sh >> /var/log/cron.err 2>&1
#
matches="server.log_*[0-9]
server_20*.log
server_access_log*[0-9]
server_access_log*[0-9]s
business.log.*[0-9]
error.log.*[0-9]
info.log.*[0-9]
other.log.*[0-9]
sf.log.*[0-9]
warn.log.*[0-9]
other.log.20*[0-9]
error.log.20*[0-9]
warn.log.20*[0-9]
info.log.20*[0-9]
business.log.20*[0-9]
server.log_20*[0-9]
sf.log.20*[0-9]"</span>
<span style="font-family: Courier New;">
Where="/data/applications/log"

gzipLimit="-mtime +30"
rmLimit="-mtime +120"

cd /

thisExec=`basename $0`
thisProc="${thisExec}[$]"
thisHost="`hostname | head -1 | cut -d\. -f1`"

DEBUG=0

for Here in ${Where}; do
        for name in ${matches}; do
                echo "`date` ${thisHost} ${thisProc} gzip ${name} in ${Here}"
                if [ ${DEBUG} -eq 1 ]; then
                    echo /usr/bin/find ${Here} -mount -type f ${gzipLimit} -name "${name}" -exec ls -al {} \;
                    /usr/bin/find ${Here} -mount -type f ${gzipLimit} -name "${name}" -exec ls -al {} \;
                else
                    /usr/bin/find ${Here} -mount -type f ${gzipLimit} -name ${name} -exec /bin/gzip -9vf {} \;
                fi
        done
        for name in ${matches}; do
                echo "`date` ${thisHost} ${thisProc} rm ${name}.gz in ${Here}"
                if [ ${DEBUG} -eq 1 ]; then
                    echo /usr/bin/find ${Here} -mount -type f ${rmLimit} -name ${name}.gz -exec ls -al {} \; -print
                    /usr/bin/find ${Here} -mount -type f ${rmLimit} -name ${name}.gz -exec ls -al {} \; -print
                else
                    /usr/bin/find ${Here} -mount -type f ${rmLimit} -name ${name}.gz -exec rm -f {} \; -print
                fi
        done
done
Share