apache httpd logfile cleanup after logrotate

A cronjob is added to cleanup log directory off huge 1GB files.  
LogRotate only rotates the files each day at 4:40Hrs.
log file is really large (upto one Gig / day).

/etc/crontab changes:

# log file deletion at /var/log/httpd/*log.? files 
#  DO IT after cron.daily/logrotate job 
05 5 * * * root perl /var/www/cgi-bin/config/utils/deleteLogFiles.pl

Pre-condtion:
error_log.1 error_log.2 etc exists.

Following perl job, upon invocation will remove above files.
   Perl’s unlink command deeltes files.
"/var/log/httpd/*log.?”-  This regular expression picks up above files.

Post-condtion:  
All files removed and logfile /var/log/httpd/deletedFiles.txt” – populated with names of files removed with time stamp. 


———————— the code ------------

use POSIX qw(strftime); 

my $logDir = "/var/log/httpd/*log.?";
my @files = glob ( $logDir ) or 
           die "Unable to get list of files for $logDir \n " ;
    
my $actionLogFile = "/var/log/httpd/deletedFiles.txt";
open (my $fd, ">>$actionLogFile") …

my $yymmdd = strftime  "%Y%m%d-%H:%M", localtime;  
print $fd "---  $yymmdd --- \n"; 

my $count = 1;
foreach (@files) {
   my $fileName = $_; 
   unlink($fileName) or die "can not delete $fileName $_\n"; 
   if ($fd) {
        print $fd "deleted $fileName \n" 
            or die "unable to append to $actionLogFile $_"; 
   }
   $count++; 
}
close $fd;

1;

Popular Posts