1. Home
  2. Computing & Technology
  3. Perl

Log Rotation - How to rotate your server log files with Perl
How to rotate your server log files with Perl

From , former About.com Guide

Next: Rotating the access log

Web servers like Apache have access and error log files, and depending on how much traffic you're dealing with these files can get quite large in a very short amount of time. Enter Perl, which is great for simple one-off system administration scripting. Instead of letting those log files build up and become unmanageable, lets write a simple Perl script to compress and archive those files every night at midnight.

To start with, we'll need to include the function strftime from the POSIX library. This will help us quickly format the current time so we can identify the archives by date. Then we define the path to the logfiles in the $log_path string. Wherever this directory is located, you should also create a directory inside it called archives and make it writeable by the user that will be running the script on cron.

Finally, we change directory into the $log_path.

#!/usr/local/bin/perl
use POSIX 'strftime';
$log_path = '/var/log/www/';
chdir $log_path;
Next: Rotating the access log
Explore Perl
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Perl log rotation tutorial - Rotating web logs with Perl, how to rotate your web server log files with a simple Perl script.>

©2009 About.com, a part of The New York Times Company.

All rights reserved.