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

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

# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit 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

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

# find httpd pid

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

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

*/5 * * * * root /root/  >/dev/null 2>&1

via cyberciti

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

Leave a Reply

Your email address will not be published.

Check out more interesting categories: Blog, Hack, HOWTO, Misc, Web, Wordpress.

Related News and Resources