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
Restart apache if website fail


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

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, ''); // Connect to your server
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20071127 Firefox/");
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_NOBODY, true);
$info = curl_getinfo($curl);

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


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


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

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.

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
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 ,121 08-24-2016, 01:25 PM
Last Post: pinguy
[How-to] reduce apache log file size 5050 0 3 ,579 01-22-2016, 12:50 AM
Last Post: 5050
{Ask} How to setting maxclients apache in linux centos 7 mbahngawean 4 15 ,581 05-29-2015, 04:47 AM
Last Post: mbahngawean

Forum Jump:

Users browsing this thread: 1 Guest(s)