The Poor Man’s Log Rotator


rotate_logs.bat:

bin\httpd -k stop

@For /F "tokens=2,3,4 delims=/ " %%A in ('Date /t') do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

ren logs logs-%Year%-%Month%-%Day%
mkdir logs

bin\httpd -k start

Instead of copying or moving large log files it’s much faster to shut down the Apache log server, rename the log folder based on the date, create a new log folder and then start up the server again. Once that process is done you’re free to copy or move the log files where ever you want without affecting the web server.

You can put this code into a bat file and use Windows Scheduler to automatically rotate your logs every week or day or whatever.

I’ve had a lot of problems with having Apache automatically name files based on the year and month to rotate logs and this seems to be a far more elegant solution. Apache does the minimum amount of work logging activity and the bat file does the minimum amount of work rotating the logs. The result is only a second or two of downtime depending on how quickly Apache stops and starts.

Leave a comment

You must be logged in to post a comment.

ss_blog_claim=70b9168863fc97c91e6d88b40542a327 ss_blog_claim=70b9168863fc97c91e6d88b40542a327