Shell Script To Auto Restart Apache HTTPD When it Goes Down / Dead

in HTTP, Web Server

Here is a simple shell script tested on CentOS / RHEL / Fedora / Debian / Ubuntu Linux. Should work under any other UNIX liker operating system. It will check for httpd pid using pgrep command

pgrep command

pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to screen. If no process found it will simply return exit status 0 (zero).

Download the script and set cronjob as follows:
*/5 * * * * /path/to/script.sh >/dev/null 2>&1

Sample script

#!/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

A better and more reliable solution is monit monitoring software for restarting services such as mysql, apache and sendmail under UNIX / Linux operating systems.

Want to read Linux tips and tricks, but don't have time to check our blog everyday? Subscribe to our email newsletter to make sure you don't miss a single tip/tricks.

{ 2 trackbacks }

Linux Server Hack - How to setup a Shell Script to Auto Restart Apache Httpd Server! | zedomax.com - Obsessively profiling DIYs, Hacks, Gadgets, Tech, Web2.0,and beyond.
July 28, 2008 at 8:51 pm
Linux Web Server Hack - How to Write Automated Load Balancing Script! | zedomax.com - The DIY, HOWTO, Hacks, Gadgets, and Tech Blog!
September 23, 2008 at 4:55 pm

{ 8 comments… read them below or add one }

sujaikumar July 3, 2008 at 7:46 am

A minor change is to be done in the above script,

The “echo $?” statement will give the exit status of the last command. In this case, if httpd is not running, the “pgrep httpd” exit status will be “non zero”.
So, it should be
if [ $? -ne 0 ] #instead of -eq
then
# restart apache
$RESTART
fi

Reply

Hubert Pollak July 7, 2008 at 2:34 am

In CentOs 5 You should use /etc/init.d/httpd restart.
Crontab would’t execute service httpd restart

regards
Hubert

Reply

vivek July 7, 2008 at 5:37 pm

Hubert,

Thanks for the heads up. The script has been updated to ‘/sbin/service’ from ’service’.

Reply

Ben July 18, 2008 at 7:18 pm

PGREP httpd returns nothing when httpd is not running, and I think that is causing the check to restart it to fail… Is there any way to test not that it returns 0 but empty or null?

Reply

vivek July 22, 2008 at 1:14 pm

Ben,

A small bug is fixed and script should work now.

Reply

Pasci August 20, 2008 at 3:12 pm


# find httpd pid
$PGREP ${HTTPD}

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

can be done in one line..

$PGREP ${HTTPD} || $RESTART

Regards pasci

Reply

Bestajaxscripts August 23, 2008 at 4:19 pm

Pasci many thx , good job, easy

and also thx to NixCraft for this shell script

Reply

Max Slevish August 26, 2008 at 10:03 pm

Nice script, works great.

I just found another good monitoring script posted on this blog: Link

The script tests the webserver’s functionality by downloading a file.

Reply

Leave a Comment

Previous post: Shell / sed Program To Remove All C and C++ Comments From Program File

Next post: NAS Backup Server Disk Monitoring Shell Script