[URGENT] Neep help in adding "killall - 9 httpd" to my cron

Discussion in 'Site & Server Administration' started by Hipto, Nov 16, 2009.

  1. #1
    Hey guys I've this php running in cron, to check httpd down status and start it again. The problem is sometimes It wouldn't start unless i use the killall manually..

    <?php
     class monitor {
     
     function httpUp() {
      $status = shell_exec("/etc/init.d/httpd status");
      
      if (preg_match("#is running#",$status)) {
       return true;
      } else {
       return false;
                           }
      
     }
     
     function restartHttp() {
      shell_exec("/etc/init.d/httpd start");
     }
     
     function writeLog($message) {
      $s = sprintf("[%s] %s\n",date("d-m-y g:i a"),$message);
                           file_put_contents("httpd.log",$s,FILE_APPEND);
     }
     
    }
     chdir("/root/");
     $monitor = new monitor();
     if ($monitor->httpUp()) {
     $monitor->writeLog("httpd already running");
    } else {
     $monitor->writeLog("httpd is down");
     $monitor->restartHttp();
     $monitor->writeLog("httpd has been started");
                         }
     ?>
    Code (markup):
    The log file goes like this
    [16-11-09 3:00 pm] httpd already running
    [16-11-09 3:11 pm] httpd already running
    [16-11-09 3:20 pm] httpd is down
    [16-11-09 3:20 pm] httpd has been started

    Can someone edit the script for me and add "killall - 9 httpd" right before it start the httpd? And so the log will look like this:

    httpd is down
    Processes have been killed
    httpd has been started
     
    Hipto, Nov 16, 2009 IP