Linux Server Hack – How to setup a Shell Script to Auto Restart Apache Httpd Server!

by max on Monday, July 28th, 2008

Well, I have been struggling with one of my dedicated servers and just found this cool script that will auto-restart the Apache httpd server if it goes out.

Save the following as restart.sh:

#!/bin/bash
# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
# RHEL / CentOS / Fedora Linux restart command
RESTART="/sbin/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"

# Httpd daemon name,
# Under RHEL/CentOS/Fedora it is httpd
# Under Debian 4.x it is apache2
HTTPD="httpd"

# find httpd pid
$PGREP ${HTTPD}

if [ $? -ne 0 ] # if apache not running
then
 # restart apache
 $RESTART
fi

Then setup a cron job like this: (usually in your sa-update file under /etc/cron.d)

*/5 * * * * root /root/restart.sh  >/dev/null 2>&1

via cyberciti

There’s also a program called Monit that will do more automation.

Categories: Blog, HOWTO, Hack, Misc, Web, Wordpress.
Advertising
Advertise Here

Leave a Reply

   Connect with Twitter

If you like this post then please subscribe to our full feed RSS or follow us on Twitter @Zedomax.

You can also subscribe by E-mail by filling out your name and E-mail below:

Name: Email:
Got a new hack, DIY, howto, or gadget you want us to blog about?

Tip us via Twitter @Zedomax or send e-mail to zedomax [at] gmail.com.

You can also use our free Guest Blogger services.

Try Goohack to find a new Hack:

Related News and Resources

Other Interesting News From Our Friends


Featured Sites From Zedomax Blog Network