Shell Script To Auto Restart Apache HTTPD When it Goes Down / Dead
Posted 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.
Download - Email this to a friend - Printable version
Discussion on This Shell Script:
Leave a Reply
We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!
Tags: apache httpd, apache web server, eq, exit status, grep command, linux, running processes, shell script, unix, Web Server ~ Last updated on: July 22, 2008



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
In CentOs 5 You should use /etc/init.d/httpd restart.
Crontab would’t execute service httpd restart
regards
Hubert
Hubert,
Thanks for the heads up. The script has been updated to ‘/sbin/service’ from ’service’.
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?
Ben,
A small bug is fixed and script should work now.
[...] via cyberciti amp, apache httpd server, apache web server, Blog, Cool, cool script, dedicated servers, fb, fedora, fi, gnu gpl version, Hack, HOWTO, Linux, linux server, lt, root root, script collection, server hack, shell script, Ubuntu, Web, Wordpress [...]
—
# 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
Pasci many thx , good job, easy
and also thx to NixCraft for this shell script
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.