Apache Restart script – Useful to auto restart after apache crash

So I thought of writing a script which will check if my website is down at regular interval and if it’s down it will restart it.

To have this setup we need following:

  1. A PHP script that will check if website is online and PHP is running fine.
  2. A shell script that will use above PHP to check if website is offline, in that case restart apache web server.
  3. Setting cron job to run at regular intervals.

PHP Script to check website status

Here is the PHP script I wrote to check my website status.

servercheck.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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);
?>

This is a very simple script which returns 1 if website is UP or 0 if it’s down.

Apache restart script

Here is the shell script I wrote to call the above PHP.

apache_restart.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/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 first calls the PHP to check website status, if it’s down then it restarts the apache server and again check the status after 10 seconds. If it’s UP then it sends a mail to the given email ID so that we know how many times server has been restarted. If restart fails, it sends a mail with error message that means something is really wrong with the server.

Make sure to give the script execute permission using chmod command and execute it once to test it. Also check the server restart command as it depends on your server operating system.

Setting Cron Job

Just add below line in the crontab entry to run the above script every five minutes. Make sure to give the correct path from root.

1
*/5 * * * * /home/journal/scripts/apache_restart/apache_restart.sh > /home/journal/scripts/apache_restart/cron/apache_restart.log 2>&1

The log file will contain the output of last run of the script. Now we are all set with the script and you will get notified whenever your server has been restarted or its in error state via mail.

Share it if you like the post and found it to be useful.

 

source: journaldev.com