May 31, 2014

Automatically rotate and clean HTTP Server log files

By default IBM HTTP Server log files always grow. This may be a problem especially on production servers where there is a large amount of HTTP requests that can quickly generate a very big access.log file. This file cannot be deleted without stopping the HTTP Server and can be practically impossible to open with a text editor.

A quick solution for this is to use the rotatelogs utility located in [HTTPHOME]/bin directory.

Open the [HTTPHOME]/bin/httpd.conf file and search for 'CustomLog' directive for access.log file. Comment this line and insert a new directive like this:
    CustomLog "|bin/rotatelogs.exe -l logs/access.%Y%m%d-%H%M%S.log 5M" common

Here is how the httpd.conf file should look like.

#CustomLog logs/access.log common
CustomLog "|bin/rotatelogs.exe -l logs/access.%Y%m%d-%H%M%S.log 5M" common

With this method a new access.log file will be generated when its size reached 5 megabytes.
More information on the rotatelog command is available here.


If you want to periodically clean the HTTP Server log files you can use the technique described hereafter.

Go to IBM HTTP Server log directory. In this example it is D:\Program Files\IBM\HTTPServer\logs.
Create an empty text file cleanlogs.cmd and paste the following code into it.

@echo off

net stop "IBM HTTP Server 6.1"
net stop "IBM HTTP Administration 6.1"

echo Waiting few seconds
ping -n 4 localhost > nul

echo Deleting log files
del /Q "%~dp0"\*log

echo.

net start "IBM HTTP Server 6.1"
net start "IBM HTTP Administration 6.1"

Test the script by manually launching it. When everything works fine you can schedule this script using Windows Task Scheduler to purge the log files each night or each Sunday.

No comments:

Post a Comment