Sunday, November 20, 2005

Been having some problem with my apache webserver lately, for some reason Apache would create another thread to listen to port 80, which makes no sense whatsoever, but netstat indicates that. As a result, I had to restart Apache like 20 times a day. Out of desperation, I wrote a little perl script to scan port 80 every minute, when it detects port 80 is down, it will attempt to restart Apache. This will at least give me some time to figure out what's going on.



use strict;
use warnings;

use IO::Socket;

my $socket= new IO::Socket::INET (
PeerAddr=>"127.0.0.1:80",
Proto=>'tcp',
Timeout=>'1' ); # Timeout.

if ($socket)
{
close $socket;
}
else
{
open(FILE, ">> portscan80.log");
print FILE "restarting apache: ", scalar localtime(time()), "\n";

open(NET, "net start|");

my $running = 0;
while () {
chomp;
if (m/Apache2/) {
$running = 1;
}
}

close FILE;
close NET;

system("net stop apache2") if $running;
system("net start apache2");
}

1;

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home