Linux How to..

Linux How to ,Share Knowledge

TAG : Shell Scripts Check Service httpd and auto restart service httpd when it down.

#create shell scripts file
vi /root/check_httpd.sh
input line below :

#!/bin/sh
/usr/bin/wget –tries=1 –timeout=30 -O /dev/null http://localhost/check_httpd.txt

if [ $? -ne 0 ]; then
(
/etc/init.d/httpd restart
datetime=`date “+%Y%m%d %H:%M:%S”`
echo ‘restarting httpd’ `ps aux` | mail -s “`hostname` restarting httpd `who | awk ‘{print $6}’`” your_email@yourdomain.com
datetime=`date “+%Y%m%d %H:%M:%S”`
echo $datetime “failure”>>/var/log/httpd/check.log
)
datetime=`date “+%Y%m%d %H:%M:%S”`
echo $datetime “Normal” >> /var/log/httpd/check.log
fi

:wq!  save and quit

#Create page for check  on DocumentRoot  Example DocumentRoot = /var/www/html/
touch /var/www/html/check_httpd.txt

#Auto Check with crontab check every time 5 minutes
crontab -e
input line below :
*/5 * * * *  /bin/sh /root/check_httpd.sh 1> /dev/null 2> /tmp/checkhttpd

:wq! save and quit

Success!!!

Comments are closed.