Monitor and restart Apache or lighttpd webserver when daemon is killed

When you cannot monitor your server for service availability, it is better to take help of automated monitor and restart utility. Last 4 days I was away from my server as I was enjoying my vacation. During this time due to load my lighttpd webserver died but it was restarted automatically within 2 minutes. I had utility configured for monitoring services on a Linux system called monit. It offers all features you ever needed for system monitoring and perform error recovery for UNIX like system.

Before monit I had my own shell and perl script for monitoring service. If service failed script will try to restart service and send an automated email to me. However monit is a superior solution.

monit is a utility for managing and monitoring processes, files, directories and devices on a Unix system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations. For example, monit can start a process if it does not run, restart a process if it does not respond and stop a process if it uses to much resources. You may use monit to monitor files, directories and devices for changes, such as timestamps changes, checksum changes or size changes.

Monit logo

You may also use monit to monitor files, directories and devices on localhost. Monit can monitor these items for changes, such as timestamps changes, checksum changes or size changes. This is also useful for security reasons you can monitor the md5 checksum of files that should not change.

Personally, I always install and configure monit on all boxes which are under my control.

Install monit under Debian or Ubuntu Linux

Use apt-get command to install monit
# apt-get install monitOR$ sudo apt-get install monit

Install monit under Red Hat enterprise Linux / CentOS Linux (source code installation)

Many distributions include monit. However monit is not included in official Red hat enterprise Linux. Just download monit source code from official web site using wget command:
# cd /opt
# wget
Untar monit
# tar -zxvf monit-4.8.2.tar.gz
# cd monit-4.8.2

Configure and compile monit:

# ./configure
# make

Install monit

# make install

Copy monit configuration file:

# cp monitrc /etc/monitrc

By default monit is located at /usr/local/bin/monit

How do I Configure monit?

monitrc is name of monit configuration file and it is by default located at /etc/monitrc location. However each distribution places file in different location: .
=> Source code installation : /etc/monitrc
=> Debian / Unentu Linux installation : /etc/monit/monitrc

Open monit configuration file and setup values as follows:
# vi /etc/monitrc

a) Run it as daemon and check the services (such as web, mysql, sshd) at 2-minute
set daemon 120

b) Set syslog logging with the ‘daemon’ facility:
set logfile syslog facility log_daemon

c) Set mail server name to send email alert
set mailserver
Set email format such as from email
set mail-format { from:
subject: $SERVICE $EVENT at $DATE

d) Now most important part, restart lighttpd or apache web server if failed or killed by Linux kernel due to any causes:
check process lighttpd with pidfile /var/run/
group lighttpd
start program = "/etc/init.d/lighttpd start"
stop program = "/etc/init.d/lighttpd stop"
if failed host port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout


  • check process lighttpd with pidfile /var/run/ : You are specifying lighttpd pid file and daemon name
  • group lighttpd: Specify group name, which is allowed or used to start/restart lighttpd
  • start program = “/etc/init.d/lighttpd start” : Command to start lighttpd server
  • stop program = “/etc/init.d/lighttpd stop” : Command to stop lighttpd server
  • if failed host port 80 : Server IP address and port number (80)
  • protocol http then restart : If above IP and port failed restart the webserver
  • if 5 restarts within 5 cycles then timeout : Try to restart 5 times; if monit cannot restart webserver 5 times; just time out to avoid race condition.

Here is my mysql server restart configuration directives:
check process mysqld with pidfile /var/run/mysqld/
group database
start program = "/etc/init.d/mysqld start"
stop program = "/etc/init.d/mysqld stop"
if failed host port 3306 then restart
if 5 restarts within 5 cycles then timeout

Here is my sshd server configuration directives:
check process sshd with pidfile /var/run/
start program "/etc/init.d/sshd start"
stop program "/etc/init.d/sshd stop"
if failed host port 22 protocol ssh then restart
if 5 restarts within 5 cycles then timeout

Here is my Apache serverrestart configuration directives:
check process httpd with pidfile /var/run/
group apache
start program = "/etc/init.d/httpd start"
stop program = "/etc/init.d/httpd stop"
if failed host port 80
protocol http then restart
if 5 restarts within 5 cycles then timeout

Replace IP address with your actual IP address. If you are using Debian just start monit:
# /etc/init.d/monit start

If you are using Red Hat Enterprise Linux, start monit from /etc/inittab file:
Open /etc/inittab file:
# vi /etc/inittab
Append following line:
mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc

Now start monit:
# init -qOR
# telinit -q

You can verify that monit is started from /var/log/messages log file:
# tail -f /var/log/messagesOutput:

Nov 21 04:39:21 server monit[8759]: Starting monit daemon
Nov 21 04:39:21 server monit[8759]: Monit started

If lighttpd died, you will see something as follows in log file:

Nov 21 04:45:13 server monit[8759]: 'lighttpd' process is not running
Nov 21 04:45:13 server monit[8759]: 'lighttpd' trying to restart
Nov 21 04:45:13 server monit[8759]: 'lighttpd' start: /etc/init.d/lighttpd

You may use monit to monitor daemon processes or similar programs running on localhost or started from /etc/init.d/ location such as
=> Apache Web Server
=> SSH Server
=> Postfix/Sendmail MTA
=> MySQL etc

Further readings

๐Ÿง Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

๐Ÿง 49 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
49 comments… add one
  • hoba Nov 29, 2006 @ 13:28

    never took the time to read through the documentation. But after reading this article I say it sounds easy enough.

    I will give it a try and probably abandon my custom setup. Thanks! ๐Ÿ™‚

  • Matthew Musgrove Nov 29, 2006 @ 18:18

    Your untar command is uncorrect. It should be:
    tar -zxvf monit-4.8.2.tar.gz

  • ๐Ÿง nixCraft Nov 29, 2006 @ 19:10

    Matthew, Thanks for heads up!

    It was a typo…

  • Jeff Dec 1, 2006 @ 17:48

    I think “inittab -q” should actually be “telinit q”.


  • ๐Ÿง nixCraft Dec 1, 2006 @ 18:28


    Yup, it was typo. You need to use init -q or telinit -q

    Appreciate your post.

  • Brian Dec 28, 2006 @ 2:18

    Does it work with Suse linux?

  • ๐Ÿง nixCraft Dec 28, 2006 @ 5:45


    Yes it does works with Sues Linux and all other UNIX like oses.

  • rick Jan 26, 2007 @ 3:08

    how do you configure monit to monitor apache’s load such that if reaches 5.00 and above it will automatically restart apache?

    thanks in advance

  • apache Feb 26, 2007 @ 14:41

    Another option, which should be a bit simpler, is to use a cron script to check the health of Apache and MySQL.

  • thecoolone Mar 4, 2007 @ 7:24

    Thanx for this tutorial. was very helpful and easy to understand.
    But i noticed one flaw/bug with monit:
    Assume that you have monit setup to monitor apache and mysql. And the section of apache comes first in the monit.conf file followed by the mysql section.
    If only apache down, monit then ONLY restarts apache (since its first in the monit.conf file). But if ONLY mysql is down(apache still running), then monit first shut’s down and restart apache and the starts mysql. So in this way i cannot make monit ONLY restart the daemon that is down.

    Any work around for this ??

  • ๐Ÿง nixCraft Mar 5, 2007 @ 14:57


    Iรขโ‚ฌโ„ขve not noticed such a bug. Try to use latest version.

  • Skullz Mar 6, 2007 @ 21:50

    any body tell me how can i get site domin name and password please brothers please am comming newly in hacking so please brothers help me am your brother please help me

  • Ira Jun 8, 2007 @ 17:25

    This document is simple and it is easy to set up. One question though.
    Can monit be set up in such way: if it can not restart a service, then send an alert to another email address, which is a pager? The line I am looking at is:

    if 5 restarts within 5 cycles then timeout

    Thanks for help in advance.

  • ๐Ÿง nixCraft Jun 8, 2007 @ 17:48

    This is default. Monit will raise an email alert when event is timeout.

    You can also try as follows:
    check process httpd with pidfile /var/run/
    alert only on { timeout, nonexist }

    monit man page has all the example, please refer the same.

  • Ira Jun 8, 2007 @ 20:36

    Thanks so much. Read through man page more.


  • jon ivar Jul 2, 2007 @ 20:40

    I have a ” something.x86 ” file that I’m running as a executable

    But I can’t find any “” anywhere

    How do I check if ” something.x86 ” is running and how do I set it up to restart if it’s not

    Best regards
    jon ivar

  • ๐Ÿง nixCraft Jul 2, 2007 @ 22:43

    us ps -aux command:
    ps -aux | grep -i 'something'

    2nd column shows PID. Generally /var/run/something stores the PID. If it is your own custom-made program, consider writing sys v init script; so that you can integrate your program with monit.


  • jon ivar Jul 3, 2007 @ 20:42

    thanks vivek

  • Zubir Jul 4, 2007 @ 7:56

    Hi all,

    How to monitor bind named server?
    Is it possible to monitor this kind of server.


  • Artem Russakovskii Dec 11, 2007 @ 2:53

    Very useful article. I was up and running on SUSE in only a few minutes.

    I completely forgot about monit and was looking for a solution to restart daemons just to find your article and realize I already know about it.

    Monit is no enterprise grade solution though, I think the latest nagios is quite nice and allows daemon restarts, custom scripts, etc. Another one I’ve seen around is zabbix. Maybe someone will find these useful.

  • hw* Dec 27, 2007 @ 6:51

    what advantages does the monit packag have compared to adding a 2 minute crontab entry that checks and restarts the daemon and sends the email alert

  • ๐Ÿง nixCraft Dec 27, 2007 @ 7:21

    Advanced configuration option
    Manage process, files, directory
    md5 security and checksum
    Read monit docs for complete feature list. I don’t think so shell script offers all these features.

  • Tim Elam Jan 4, 2008 @ 22:34

    Thanks for a great article. Just what I was looking for. FYI, Monit 4.10.1 is out now.

  • paul Mar 26, 2008 @ 7:11

    I have installed monit on my server. But here a problem happened, such if I wants to run a process,such as “/root/appname -m ‘pram1′”, it wokrs, but if I write it in a executable Shell file, it cannot be executed. Why?

  • paul Mar 28, 2008 @ 4:07

    It’s me, the last problem is resolved, it has something to do with working path. But here other question.: why I cann’t excute a shell files, the content like this:
    1 cd /root/test
    2 ./appname -m “param1”

  • Marcus Herou Apr 30, 2008 @ 8:34


    My lighty instance only return 400 to monit. It is really strange since my app server with almost the same monit setup work flawlessly.

    Any ideas ?

    lighttpd-1.4.19 (compiled)
    monit-4.8.1 (ubuntu-hardy)

  • yara Jun 13, 2008 @ 8:00

    I have debian with apache2 compiled and mysql 5, i don’t have /var/run/ , how can chacke my process :

    check process httpd with pidfile .....

  • ๐Ÿง nixCraft Jun 13, 2008 @ 10:06

    Under Debian it is /var/run/ file.

  • drewish Jun 15, 2008 @ 20:49

    For Debian Linux see monitrc config file.

  • woodsb02 Aug 31, 2008 @ 19:06

    I had the same problem as Marcus Herou. My lighttpd is showing as “Connection failed” and when I look in my access.log I see that lighttpd keeps returning 400. I have no idea why.

  • Cjso Dec 12, 2008 @ 18:33

    What are the reasons monit can not execute /etc/init.d/httpd service?

    httpd is part of ‘root’ group.


  • whatvn Jan 1, 2009 @ 16:43

    you miss “s” in command:
    “tail -f /var/log/message”, it should be “tail -f /var/log/messages”

    Thank so much for awesome post.

  • ๐Ÿง nixCraft Jan 1, 2009 @ 17:01

    Thanks for the heads up.

  • unix newbie Jan 7, 2009 @ 0:52


    I am a newbie and am having problems getting monit to work. I have gotten all the way to the following command but I get stuck after this:

    mo:2345:respawn:/usr/local/bin/monit -Ic /etc/monitrc

    I inserted this command into the inittab file but after that I can not simply copy and paste “init -q” into the shell terminal. I get an —INSERT— prompt and then anything I type after doesnt matter.

    I know very little about Unix commands other than copying and pasting and what I can dig up on a quick google search. Im figuring that I am doing something or dont know something very elementary and would just like to get this to work.

    Thank you guys/gals in advance

  • desis Jan 21, 2009 @ 22:36

    I have installed on redhat Centos 5..I have changed the inittab file… but after that when I used # tail -f /var/log/messages command …I dont get the output …as describe here…actually I dont get any output related with monit … any help is appreciated … I need to install it badly… is there something I am missing..

  • Alexander D Jan 22, 2009 @ 23:06

    first of all thanks for great manual. THe problem is, that i did as i descrbed here, everything was ok … until this

    Now start monit:
    # init -qOR
    # telinit -q

    Goting such output
    [root@server etc]# init -q
    init: invalid option -- q
    Usage: init 0123456SsQqAaBbCcUu
    [root@server etc]# telinit -q
    telinit: invalid option -- q
    Usage: telinit 0123456SsQqAaBbCcUu

    and of course all steps after this

    f.e. for this command

    # tail -f /var/log/messages
    server stop for a minute, … i stop it manually …
    later check that my messages file become ~16mb

    so, for now Monit don’t monitor, don’t send any alert … and don’t know what to do ?

    try to find any solution, or may be unistall it

  • Alexander D Jan 22, 2009 @ 23:08

    Forgot to mention that after this

    got such output

    Jan 22 13:32:54 server sshd[10743]: subsystem request for sftp
    Jan 22 13:46:45 server proftpd[11262]: ([]) - FTP session opened.
    Jan 22 15:02:44 server sshd[13648]: Accepted password for root from ::ffff: port 14969 ssh2
    Jan 22 15:16:32 server sshd[18913]: Accepted password for root from ::ffff: port 15068 ssh2
    Jan 22 15:16:33 server sshd[18913]: subsystem request for sftp
    Jan 22 15:19:03 server sshd[18985]: Accepted password for root from ::ffff: port 15069 ssh2
    Jan 22 15:19:04 server sshd[18985]: subsystem request for sftp
    Jan 22 15:23:33 server sshd[19137]: Accepted password for root from ::ffff: port 15070 ssh2
    Jan 22 15:23:33 server sshd[19137]: subsystem request for sftp
    Jan 22 15:44:14 server sshd[19743]: Accepted password for root from ::ffff: port 15308 ssh2

    btw isn’t my iP ๐Ÿ™

  • Mfawa Alfred Onen May 29, 2009 @ 19:05

    Great Post nixcraft. Just Check on Some of your Typos. Keep it Up!

  • Martin Jun 20, 2009 @ 16:09

    Gracias por la Guia, me ha sido de gran ayuda ๐Ÿ™‚

  • Mohd Anas Oct 26, 2009 @ 10:41


    Can someone tell me how to monitor vsftpd.
    I’m not getting its pid file. I’m using CentOS 5.3.


  • Yogesh Feb 19, 2010 @ 13:24

    @ Mohd. Anas –> VSFTP can’t be monitored using MONIT. Reason is as what you already know… it doesn’t create PID file ๐Ÿ˜‰
    And MONIT uses PID file to learn about running process…. ๐Ÿ™‚

    @ Vivek –> I am a big NixCraft fan. Sorry to advice you but please update the document for monitoring and managing services from a web interface.

    # vi /etc/monitrc
    set httpd port 2812 # To bind monit on port 2812 of apache
    allow admin:monit # require user ‘admin’ with password ‘monit’

    Restart monit to apply changes then open http://:2812 in web-browser.

  • 3dex May 6, 2010 @ 1:22

    Searched the net for days until I’ve found this thread and understand I must replace the localhost with the server’s IP for Apache monitoring to work… but, better late then never ๐Ÿ™‚
    Thanks for the intel!

  • Gobnd Singh Aug 17, 2010 @ 15:00

    Hello All,
    I have configured as told by above manual , however i don’t know how to check that weather its working or not.
    can somebody guide me. how can i test that monitor is working or nit.

  • Chris Walker Aug 29, 2010 @ 1:42

    It appears the new version 5.1.1 has a bit of a different structure than explainewd here.
    I get some errors with my configuration like mo repeated to fast disabling for 5 minutes, and an unmonitor error.

    It does however, appear to be functioning, as I can watch what users login, in real time, and various SPI activity. However, I don;t know if it is monitoring the service properly to restart it, when needed.

  • Rocky Nov 25, 2010 @ 12:10


    I want to configuer monit to only generate alert mail for service monitoring and want to monitor only service.
    How to configure it……..?
    Because when i see monit status it showas service and local system statistics.


  • screenmates Mar 11, 2012 @ 22:04

    What if the service died but the pid file never got deleted?

  • anacondaweb Apr 27, 2012 @ 14:57

    Excelente guรƒยญa.
    Gracias por compartirla.

  • Carlos Baez Apr 21, 2016 @ 13:34

    Can this be used with Asterisk?

  • Christopher Apr 28, 2016 @ 15:42

    Just because the port is open and the pid exists does not mean that a service is ‘running’ the true test for responsiveness and usability is as close to using the thing as possible… though I appreciate the tool I’ll stick with curl to check the actual site.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum