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

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.

Featured Articles:

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

{ 18 comments… read them below or add one }

1 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

2 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

3 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

4 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

5 vivek July 22, 2008 at 1:14 pm

Ben,

A small bug is fixed and script should work now.

Reply

6 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

7 Bestajaxscripts August 23, 2008 at 4:19 pm

Pasci many thx , good job, easy

and also thx to NixCraft for this shell script

Reply

8 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

9 shuji August 14, 2009 at 3:51 am

hi all,
i wan to ask, i already run the command ” $ crontab -e ” at the terminal.
so is it i copy the whole sample scripts into the crontab file and then save it?
that all??
for your information im newbie in unix.
Please help. :(

regards
Shuji

Reply

10 Shrii August 21, 2009 at 8:00 am

@shuji
Follow this:
vi /usr/bin/httpdmonitor
copy the script in to that, after that chmod 777 /usr/bin/httpdmonitor
Now set corntab as:

crontab -e
*/5 * * * * /usr/bin/httpdmonitor >/dev/null 2>&1

This is good script.
Thanks Vivek-
Shrii

Thanks-

Reply

11 lusius November 4, 2009 at 12:19 pm

./root/monitoringapache.sh: line 5: /sbin/service httpd restart: No such file or directory
pgrep: No matching criteria specified
Usage: pgrep [-flvx] [-d DELIM] [-n|-o] [-P PPIDLIST] [-g PGRPLIST] [-s SIDLIST]
[-u EUIDLIST] [-U UIDLIST] [-G GIDLIST] [-t TERMLIST] [PATTERN]
./root/monitoringapache.sh: line 14: $: command not found
./root/monitoringapache.sh: line 16: [127: command not found

line 14 is
$PGREP $ { HTTPD }
line 16 is
if [ $? -ne 0]

I have this troubles im working on redhat please help me

Reply

12 lucious November 4, 2009 at 4:03 pm

I make run this script and wen i stop my apache, this $PGREP ${HTTPD}
still geting cero and never restart the apache service, there are another way to see if apache is running?

Reply

13 mohsin November 7, 2009 at 4:22 pm

i’m getting

“[0: command not found"

for the

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

any ideas?

Reply

14 luka December 6, 2009 at 1:19 am

Try ed it for checking deamon of one custom script and works like a charm.Those who says how they get command not found i guess they don’t have command PGREP installed on their OS or they set wrong PATH.

Reply

15 Guilherme Carvalho January 4, 2010 at 4:45 pm

I got a problem, sometimes my httpd stop but the PID still alive, and i need to restart manually the httpd, how can i fix this?? When it happens, i run the httpdmonitor and appears only one PID, so i need to restart apache, then i run the httpdmonitor again, and i can see many PID from apache.

Please help me, i´m needing to restart the apache almost 6 times a day.

Thanks

Reply

16 jitz January 4, 2010 at 9:04 pm

I saved the script to httpdmonitor.sh and then when it gets called from the script pgrep was not doing a whole word search and returned trued even thought httpd was turned off. This is on an older redhat system. change pgrep to be ‘pgrep -l -x’. Now it appears to work well and ignores httpdmonitor.sh

FYI for anyone else who runs into this snag.

Regards

Reply

17 Andreas January 5, 2010 at 11:48 pm

I put this script to github so everybody may clone and change it.

http://github.com/andreashe/keepapacherunning

Tell me if this is not ok and I will remove it.

Maybe someone want to build in some autodetections for differences in distries.

Reply

18 Vivek Gite January 8, 2010 at 6:47 am

It is a GPLed script, so I don’t see any problem as long as anyone follows GPL.

Reply

Previous post:

Next post: