rotate apache logs problem

I'm using the following script in my cron every night at 12am. But the new logs never get written to after the old logs are moved and compressed. If I restart apache in the morning then the logs start getting filled again. What can I do to fix this?

Thanks


Code:
#!/usr/bin/perl

use Cwd;

my ($logfile,$path,$filename);

open LIST, "< " . "/export/home0/maintenance/yesterday.txt" or die "Cannot open list file! - $!\n";

while (<LIST>) {
  $logfile = $_;	# read a line
  chomp $logfile;	# cut off the trailing newline character

  if (($logfile !~ /^\s*#.+/) && ($logfile !~ /^\s*$/)) {
    print "++$logfile\n";
    if (-e $logfile) {
      ($path,$filename) = $logfile =~ m|^(.*[/\\])([^/\\]+*beep*?)$|;

      my @stats = stat($logfile);    
      my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($stats[9]);
      my $filedate = sprintf("%d%02d%02d",$year+1900,$mon,$mday);
  
      my $newname = $filename . "." . $filedate . ".gz";
      my $dstfile = $path . $newname;

      my $filenum = 1;
      print "--$dstfile\n";
      if (-e $dstfile) {
        while (true) {
          # make a new destination file name
          $newname = $filename . "." . $filedate . "-" . $filenum . ".gz";
          $dstfile = $path . $newname;
          if (-e $dstfile) {
            $filenum++;
          } else {
            last;
          }
        }
      }
  
    
      system("mv -f $logfile $dstfile");
  
      # print the working message, remove the old log file, and create a new empty one
      print "creating backup: $dstfile\n";
      

    }
  }
}
system("apachectl graceful");

# close the log file list
close LIST;

open LIST, "< " . "/export/home0/maintenance/xlogrotate.lst" or die "Cannot open list file! - $!\n";
while (<LIST>) 
{
  $logfile = $_;	# read a line
  chomp $logfile;	# cut off the trailing newline character
     if (($logfile !~ /^\s*#.+/) && ($logfile !~ /^\s*$/)) 
     {
        if (-e $logfile) 
        { 
           
           ($path,$filename) = $logfile =~ m|^(.*[/\\])([^/\\]+*beep*?)$|;
           $newname = "$path$filename.yesterday"; 
           print "moving $logfile to $newname\n";
           system("mv -f $logfile $newname");
           system("gzip $newname");
           system("touch $logfile");
        }
     }
}

# restart the apache service using graceful (existing connections aren't terminated)
print "restarting apache...\n";
system("apachectl graceful");
system("perl /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -config=pexxx -update");
 
I was using that first and had problems. I don't know if it is because i am using awstats that grabs the rotated log and tries to process it too quickly. It was rotating the log, but somehow I ended up with a tiny log for a few lines, and then another log. Also, the thing names the log files .1, .2, .3 etc and I need the date incorporated in each one.

It is very frustrating for me, I have ubuntu boxes that i set up and they just work, automatically rotate the logs out of the box.
 
On Linux the apache logfile is rotated via a small cron script and this script also send apache a HUP signal so a new logfile will be created.

You can do the same in your script, just rename the file and then send apache a HUP signal, now apache creates the logfile again and you can do further steps in your script.
Maybe an alternative is cronolog (I don't use apache without this tool)
 
Apache can be configured to dump the log into a pipe, you can simply write your own script that will write and rotate the log file on other end of the pipe, or you might want to google around and see if someone already has made one so you don't have to invent the wheel.
 
This is my rotate script xD
Code:
# grep httpd /etc/newsyslog.conf
/store/log/*log         root:wheel      644  10    *    @01T03 GJC   /var/run/httpd.pid 30
 
Back
Top