John’s Oracle Experiences

My everyday experiences with Oracle products

Archive for January 19th, 2009

Easy (Apache) logfile clean-up script

Posted by John Paul van Helvoort on January 19, 2009

Here is a script to cleanup Apache logfiles which exceed 100 Megabytes.
This script was originally created to prevent the logfiles to grow over 2 Gigabytes as this would
crash the Apache server process. As it turned out this script can be used in many cases to control the growth of logfiles.

A report is send as a confirmation of the cleaned logfile which looks like this :

----------------------------------------------------------------
Checking Logfiles Apache in : /var/log/apache2 on web
Files with size : 100000 Kb are cleared
----------------------------------------------------------------
CLEANING : /var/log/apache2/web_ssl_access.log
----------------------------------------------------------------

Here is the actual script :

#!/bin/sh
#####################################################################################################
#
# Check Logfiles script for Apache server
# John Paul van Helvoort
#
#####################################################################################################
export PATH=/usr/local/bin:/usr/bin:/bin
HOSTNAME=`uname -a | cut -f2 -d" "`
DAY=`date "+%A"`
APACHEPATH=/var/log/apache2
MAILADRES=sys@backbone.local
SIZE=100000

mail_output() {
 /usr/bin/mailx -s "Check Apache Logfile on $HOSTNAME" $MAILADRES < $APACHEPATH\logging.log
}

clean_logging() {
 > $APACHEPATH\logging.log
}

for LOG in `find $APACHEPATH -name "*.log" -size +$SIZE -exec ls {} ';'`
  do
  clean_logging
  if [ "$LOG" != "" ]; then
    echo "---------------------------------------------------------------- "    >> $APACHEPATH\logging.log
    echo "Checking Logfiles Apache in : $APACHEPATH on $HOSTNAME            "    >> $APACHEPATH\logging.log
    echo "Files with size : $SIZE Kb are cleared                           "    >> $APACHEPATH\logging.log
    echo "---------------------------------------------------------------- "    >> $APACHEPATH\logging.log
    echo CLEANING : $LOG  >> $APACHEPATH\logging.log
    echo CLEANING : $LOG
    > $LOG
  fi
  echo "---------------------------------------------------------------- "  >> $APACHEPATH\logging.log
  mail_output
done

Posted in Application Server | Leave a Comment »