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

in Categories HTTP, Web Server last updated July 22, 2008

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

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


Share this on:

27 comment

  1. I’m looking to modify this to tie into the screensaver and end the session running/ logout current user for a kiosk installation. In other words, how could this script be modified to check for screensaver initiating and then logout/restart the session instead of restarting apache?
    Thanks in advance,
    Taylor G.

  2. 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

  3. 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

  4. 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.

  5. 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?

  6. ./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

  7. @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-

  8. 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

  9. 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.


  10. # 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

  11. 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?

  12. Hubert,

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

  13. 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

    Have a question? Post it on our forum!