Thursday, July 3, 2008

Shell script to auto restart web server httpd if it goes down

First set the crontab as follows
*/5 * * * * /path/to/script.sh >/dev/null 2>&1

Shell script as follows

#!/bin/bash
# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
# -------------------------------------------------------------------------
# RHEL / CentOS / Fedora Linux restart command
RESTART="service httpd restart"

# uncomment if you are using Debian / Ubuntu Linux
#RESTART="/etc/init.d/apache2 restart"

#path to pgrep command
PGREP="/usr/bin/pgrep"

# find httpd pid
$PGREP httpd

if [ $? -eq 0 ]
then
# restart apache
$RESTART
fiA better and more reliable solution is monit monitoring software for restarting services such as mysql, apache and sendmail under UNIX

1 comment:

melain said...

Thanks For this Tutorial now i know how this Shell Script works
Please visit my site and Rate my Skills on Design?

Visit my Website