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/ >/dev/null 2>&1

Sample script

# Apache Process Monitor
# Restart Apache Web Server When It Goes Down
# -------------------------------------------------------------------------
# Copyright (c) 2003 nixCraft project <>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit 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
# Httpd daemon name,
# Under RHEL/CentOS/Fedora it is httpd
# Under Debian 4.x it is apache2
# find httpd pid
if [ $? -ne 0 ] # if apache not running 
 # restart apache

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 via:
Category List of Unix and Linux commands
Disk space analyzers ncdu pydf
File Management cat
Firewall Alpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilities NetHogs 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 Alpine 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” ]
    return 1

    isapup /opt/httpd/2.4.0

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

  • 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


    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/ >/dev/null 2>&1

  • TDG88UGA Mar 19, 2010 @ 13:25

    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.

  • Andreas Jan 5, 2010 @ 23:48

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

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

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

    • 🐧 Vivek Gite Jan 8, 2010 @ 6:47

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

  • jitz Jan 4, 2010 @ 21:04

    I saved the script to 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

    FYI for anyone else who runs into this snag.


  • Guilherme Carvalho Jan 4, 2010 @ 16:45

    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.


  • luka Dec 6, 2009 @ 1:19

    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.

  • mohsin Nov 7, 2009 @ 16:22

    i’m getting

    “[0: command not found”

    for the

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

    any ideas?

  • lucious Nov 4, 2009 @ 16:03

    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?

  • lusius Nov 4, 2009 @ 12:19

    ./root/ 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]
    ./root/ line 14: $: command not found
    ./root/ 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

  • Shrii Aug 21, 2009 @ 8:00

    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-


  • shuji Aug 14, 2009 @ 3:51

    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. :(


  • Max Slevish Aug 26, 2008 @ 22:03

    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.

  • Bestajaxscripts Aug 23, 2008 @ 16:19

    Pasci many thx , good job, easy

    and also thx to NixCraft for this shell script

  • Pasci Aug 20, 2008 @ 15:12

    # find httpd pid

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

    can be done in one line..


    Regards pasci

  • vivek Jul 22, 2008 @ 13:14


    A small bug is fixed and script should work now.

    • Anonymous Oct 3, 2011 @ 2:11

      what bug was fixed? is the orginal source updated?

  • Ben Jul 18, 2008 @ 19:18

    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?

  • vivek Jul 7, 2008 @ 17:37


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

  • Hubert Pollak Jul 7, 2008 @ 2:34

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


  • sujaikumar Jul 3, 2008 @ 7:46

    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
    # restart apache

Leave a Reply

Your email address will not be published.

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