This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Restart apache if website fail
#1
Restart apache if website fail
Hi,

Source:

http://www.journaldev.com/574/apache-res...ache-crash

This script polls a website to check it's responding usin php:
servercheck.php

Quote:<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.journaldev.com'); // Connect to your server
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11");
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_exec($curl);
$info = curl_getinfo($curl);

// Check server's state
if ((!curl_error($curl)) && ($info['http_code'] != 0)) {
echo 1;
} else {
echo 0;
}

curl_close($curl);
?>

It will return 1 or 0. It will be run using:
apache_restart.sh

Quote:#!/bin/bash

# Make sure you make these paths correct
result=`/usr/local/bin/php /home/journal/scripts/apache_restart/servercheck.php`
echo $result
if [ $result != 1 ]
then
/etc/init.d/httpd restart
sleep 10
restartcheck=`/usr/local/bin/php /home/journal/scripts/apache_restart/servercheck.php`
if [ $restartcheck == 1 ]
then
echo "Apache server have been successfully restarted!" | mail -s "Server Alert: Apache restarted" pankaj.kumar@journaldev.com
exit
else
echo "Apache server is still in error state!" | mail -s "Server Alert: Apache in error state" pankaj.kumar@journaldev.com
exit
fi
exit
fi

This script could be tailored to add a mySQL restart or detect mySQL errors too. Or even to check response delay to trigger other custom responses and automates some admin task.

M B
No support using PM (Auto adding to IGNORE list!), use the forum. 
How to ask
Freelance AWS Certified Architect & SysOps// DevOps

10$ free to start your VPS
Reply
Thanks given by: ReinforcedPanda


Possibly Related Threads…
Thread Author Replies Views Last Post
Monitoring running services and restart them if they stop working on Ubuntu. pinguy 1 7 ,091 08-24-2016, 01:25 PM
Last Post: pinguy
[How-to] reduce apache log file size 5050 0 3 ,559 01-22-2016, 12:50 AM
Last Post: 5050
{Ask} How to setting maxclients apache in linux centos 7 mbahngawean 4 15 ,514 05-29-2015, 04:47 AM
Last Post: mbahngawean

Forum Jump:


Users browsing this thread: 1 Guest(s)