couldn't edit my above post, because i posted it over 15 minutes ago. anyway, here is PHP code i wrote to restart apache if its down.
now, set a cron job to run it, in the following format
/path/to/php/binary /path/to/php/file > /dev/null
the script outputs error that it encounters with apache, and the time it happened. so if you want to know when apache died, use this command in cron instead:
/path/to/php/binary /path/to/php/file > /path/to/log/file
now, remember, your restarting apache, so you need to run the script as the user you normal run apache under, or restart apache under.
anyway, here is the code i promised:
PHP Code:
<?php
/*****************************************/
/*** Apache Response Checking ***/
/*****************************************/
/*
** First, i need to know a bit about the server.
** give me a domain, and a file to download.
** and the command used to restart apache.
*/
// Domain to connect to:
$test_domain = "thedigitaldream.co.uk";
// File to download:
$test_file = "index.php";
// Command to restart apache
$restart_cmd = "apachectl restart";
/*****************************************/
/*** You wont need to modify anything ***/
/*** past this point. ***/
/*****************************************/
/*
** Store current time in a var, used for logging
*/
$current_time = gmDate("H:i - d/m/y");
/*
** Create a connection to the server. if this
** fails, we cant even connect to the host.
** so we will force the apache restart.
*/
if(!$apache_handle = @fsockopen($test_domain, 80))
{
// Apache is down, restarting it.
$result = shell_exec($restart_cmd);
echo($current_time." - Cant connnect to apache, Restarting Apache\n");
}
else
{
// We are connected to apache, great.... lets
// see if we can grab the test file from the
// server though.
// Generate a request with the above information
$http_request = "GET ".$test_file." HTTP/1.1\r\n";
$http_request .= "Host: ".$test_domain."\r\n\r\n";
// Send the request to the server:
fputs($apache_handle, $http_request);
// Get back the response (if any).
$http_response = fgets($apache_handle);
if(!$http_response)
{
// No response back, lets restart apache
$result = shell_exec($restart_cmd);
echo($current_time." - No http response, Restarting apache\n");
}
// End Of Test
}
/*
** Coded on 28th, Feb, 2004 by Matthew Jones.
*/
?>
i can re - write this in perl, or c++ if needed.
Matt.