1. Computing & Technology

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

©2012 About.com. All rights reserved.

A part of The New York Times Company.