# rotate apache logs problem



## scottemick (Dec 30, 2010)

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



```
#!/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");
```


----------



## phoenix (Dec 30, 2010)

Use newsyslog.conf(5) instead, as it comes with the system, includes support for GZip and BZip compression, and handles sending the HUP signal to the process to get it to use the new log files.


----------



## scottemick (Dec 30, 2010)

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.


----------



## ohauer (Dec 30, 2010)

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)


----------



## expl (Dec 30, 2010)

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.


----------



## Alt (Dec 31, 2010)

This is my rotate script xD

```
# grep httpd /etc/newsyslog.conf
/store/log/*log         root:wheel      644  10    *    @01T03 GJC   /var/run/httpd.pid 30
```


----------

