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.

Get the latest tutorials on SysAdmin, Linux/Unix, Open Source, and DevOps topics:
CategoryList of Unix and Linux commands
File Management cat
Firewall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities dig host ip nmap
OpenVPN CentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Manager apk apt
Processes Management bg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searching grep whereis which
User Information groups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPN CentOS 8 Debian 10 Firewall Ubuntu 20.04
27 comments… add one
  • Wolfsrudel May 4, 2012 @ 8:34

    Why won’t you just use ‘monit’?

  • francois Apr 13, 2012 @ 10:17

    I am not a big fan of greping httpd in process list to see if httpd server is running or not. There can be several httpd services installed. Two options are either the status module, or this bash script to the extent that you known the full path to your httpd bin folder:

    function isapup(){
    pid=$($1/bin/apachectl -S | grep PidFile | awk ‘{print $2}’) # retrieve the expected path of the process id file
    pid=${pid:1:`expr ${#pid} – 2`} # get rid of the quotes
    up=$(ls $pid 2> /dev/null) # see if process id file exists

    if [ ! “$up” ]
    then
    return 1
    fi
    }

    isapup /opt/httpd/2.4.0

    if [ $? -eq 1 ] # if isapup returned 1
    then
    echo Starting Apache
    /opt/httpd/2.4.0/bin/apachectl start
    fi

  • Phillip Oct 3, 2011 @ 2:34

    Had to use PGREP=”/usr/bin/pgrep -l -x” to get it to work on CentOs (5.4)

  • John Mar 13, 2011 @ 15:43

    If adapting this for other processes add an -x flag to force pgrep to match the process name exactly

    $PGREP ${HTTPD}

    The name of my script contains the name of the the process to pgrep was giving a false positive when the process wasn’t running.

  • Dlugi Nov 4, 2010 @ 9:07

    You made mistake in crontab entry. It will not work.

    You should specify user which will execute the script:

    This crontab is correct (CentOS):

    */5 * * * * root /path/to/script.sh >/dev/null 2>&1

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.