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
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");