Tutorial

How To Install and Configure Monit

Published on August 31, 2012
How To Install and Configure Monit

About Monit

Monit is a helpful program that automatically monitors and manages server programs to ensure that they not only stay online consistently, but that the file size, checksum, or permissions are always correct. Additionally monit comes with a basic web interface through which all of the processes can be set up. This tutorial will cover the most basic setup and configuration.

Install Monit

Monit is easiest to install through apt-get:

sudo apt-get install monit

Once monit downloads, you can add programs and processes to the configuration file:

sudo nano /etc/monit/monitrc

Monit can be started up with a command that then keeps it running in the background

monit

Typing monit status displays monit’s details:

The Monit daemon 5.3.2 uptime: 1h 25m 

System 'myhost.mydomain.tld'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.03] [0.14] [0.20]
  cpu                               3.5%us 5.9%sy 0.0%wa
  memory usage                      26100 kB [10.4%]
  swap usage                        0 kB [0.0%]
  data collected                    Thu, 30 Aug 2012 18:35:00

Configure Monit

Monit is very easy to use nearly out of the box. By default, it is set up to check that services are running every 2 minutes and stores its log file in “/var/log/monit.log”.

These settings can be altered at the beginning of the configuration file in the set daemon and set logfile lines respectively.

Web Service

Monit comes with it’s own web server running on port 2812. To configure the web interface, find and uncomment the section that begins with set httpd port 2812. Once the section is uncommented, write in your server’s IP or domain name as the address, allow anyone to connect, and then create a monit user and password

set httpd port 2812
    use address 12.34.56.789  # only accept connection from localhost
    allow 0.0.0.0/0.0.0.0        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'

Once this is configured, monit should reload and reread the configuration file, and the web interface will be available:

monit reload

You will then be able to access the monit web interface by going to “example.com:2812”

Login with your chosen username and password. Your screen should look something like this.

Configuring Programs Self-Monitoring

Once the web services are set up, you can begin to input the programs that you want monitored and protected into the "/etc/monit/monitrc" configuration file. To simply ensure that programs stay online, you can use the /etc/init.d commands to stop or start a program.

Here are some example configurations:

Apache:

check process apache with pidfile /run/apache2.pid
    start program = "/etc/init.d/apache2 start" with timeout 60 seconds
    stop program  = "/etc/init.d/apache2 stop"

MySQL

check process mysqld with pidfile /var/run/mysqld/mysqld.pid
    start program = "/etc/init.d/mysql start"
    stop program = "/etc/init.d/mysql stop"

Nginx

check process nginx with pidfile /var/run/nginx.pid
    start program = "/etc/init.d/nginx start"
    stop program = "/etc/init.d/nginx stop"

Finish Up

Once you have configured all of the programs that you want to run, they will be automatically tracked and restarted should they turn off.

You can control the programs through both the web interface or the command line.

Once you have set up the configuration, check the syntax:

monit -t

After resolving any possible syntax errors, you can start running all of the monitored programs.

monit start all
By Etel Sverdlov

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the author(s)

Etel Sverdlov
Etel Sverdlov
See author profile
Category:
Tutorial

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
52 Comments
Leave a comment...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I’ve following this article but I’m getting an “Execution failed” status for apache. It doesn’t look like the PID file exists in: /usr/local/apache/logs/httpd.pid

(Ubuntu 12.04)

You can try using the following command:

echo $(. /etc/apache2/envvars && echo $APACHE_PID_FILE) 

It will give you the correct PID file location. In my case it was /run/apache2/apache2.pid.

Etel Sverdlov
DigitalOcean Employee
DigitalOcean Employee badge
September 20, 2012

There was a typo in the configuration. I have corrected it, please let me know if you continue having issues.

i’m having the same error ? (ubuntu 14.04.1) help thanks

ok, i solved it. ist working :-)

Cheers, that’s done it!

Hi.

I installed monit as above on 12.04 & it’s running but I get this (names changed to protect the innocent):

`$ ps aux | grep monit root 1193 0.0 0.0 5492 832 ? S Sep12 1:27 /usr/bin/monit -c /etc/monit/monitrc fubar 12127 0.0 0.0 4372 812 pts/1 S+ 02:05 0:00 grep --color=auto monit

$ sudo monit status [sudo] password for fubar: monit: error connecting to the monit daemon`

I’ve checked /etc/monit/monitrc & it looks good enough to get things going & as seen above I think it’s running…

Any help appreciated

sorry to answer my own question but in order to get rid of that error message & see monit’s status, I had to enable the httpd section in /et/monit/monitrc even though I wasn’t using the web interface (apparently you have to set the monit httpd process to allow connections from localhost).

Hope this helps someone else late at night while searching Google (it’s 02:42 in London ;-)

I have followed this tutorial and I keep getting this message:

monit: activation failed – There is no service by that name monit: activation failed – There is no service by that name

I copied and pasted the congfig for apache and mysql and tried again and still got the same messages.

do you have any ideas about what I have done wrong.

Excellent article, got a test forum running on Digital Ocean in around 1h and used this command to keep thin running after the terminal window closes which I found useful:

thin start -d

macrus.hann6: after your make changes to the config file, don’t forget to run $ monit reload. Also make sure the actual pid files exist at that location.

When I login as a user and run Monit I get “monit: The control file ‘/etc/monit/monitrc’ must be owned by you.”

The file is owned by root, and when I login as root and run monit nothing happens.

So then I change the file to be owned by the user and then get “monit: Error opening the log file ‘/var/log/monit.log’ for writing – Permission denied”

After changing the owners on a lot of files eventually I don’t get any errors, and I also don’t see Monit.

Running monit status gives me “monit: error connecting to the monit daemon”

I am puzzled.

I’m experiencing some troubles with firewall configuration for the web access. i’ve set firewall as described here: https://www.digitalocean.com/community/articles/how-to-set-up-a-firewall-using-ip-tables-on-ubuntu-12-04

if i try to add sudo iptables -A INPUT -p tcp --dport 2812 -j ACCEPT (output too)

i’m unable to access. if i flush the firewall and remove all rules i’m able to access.

any idea? what i’m doing wrong?

i solved with:

sudo nano /etc/iptables/rules.v4 inserting this in the line i needed -A INPUT -p tcp -m tcp --dport 2812 -j ACCEPT

I followed the above steps and saw the same issue as Adam Patterson. I changed the owner of monitrc to the logged-in user and now on start I get a similar but less specific error: “error : Cannot open for write – Permission denied” I made sure the monit.log was owned by the same user as monitrc and /usr/bin/monit. I’m not sure what else I might need to modify.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 17, 2013

@ryankbrown: Is there any reason you do not want to run monit as root?

I followed the above steps and saw the same issue as Adam Patterson. I changed the owner of monitrc to the logged-in user and now on start I get a similar but less specific error: “error : Cannot open for write – Permission denied” I made sure the monit.log was owned by the same user as monitrc and /usr/bin/monit. I’m not sure what else I might need to modify.

Ummm… Not sure how I re-posted the original question…

Well, I can certainly run monit as root. It is a general policy here to use a sysadmin account. (Not sure, not my policy) but I can likely get around it if that is a problem.

Thanks you.

How about CentOS?

I want to use Monit for Unicorn_rails instances From which user i should run unicorn (deploer || root) ? How to achive restarting application through monit after each deploy?

I love the fact that every article im searching for i able to find on digitalocean… great service btw!

hi i really loved ‘monit’,

i tested in my localhost and it is working fine,but i want to install and configure it in my website server. i tried to put my website name like this by connecting to the server using ssh

set httpd port 2812 use address abc.website.com # only accept connection from localhost allow localhost # allow localhost to connect to the server and allow admin:admin # require user ‘admin’ with password ‘monit’

but when trying to open the website in browser as abc.website.com:2812 nothing happens loading loading please help me

Looks like you have to change the allow localhost for:

allow 0.0.0.0/0.0.0.0

Cheers

Same here, MyIp:2812 does not give anything.

I have installed this with no problem in the past but for some reason monit isn’t doing anything. I type monit -t and get no feedback. monit reload does nothings. And the web interface is unavailable. I have tried removing the package and reinstalling with no luck. monit status also just returns without printing anything.

I get this when doing monit status: monit: cannot read status from the monit daemon

Got the same “error connecting to the monit daemon” message upon “monit status” even though monit’s httpd was configured, the port was open, and I could approach the httpd server with wget/lynx. It turned out the solution was to tell monit where the conf file is: monit -c /etc/monitrc status

So if I have elasticsearch running in my etc/elasticsearch. What would my command be to keep it running?

And I put this is my “”/etc/monit/monitrc" file.

I didn’t understand the bit about “/etc/init.d” commands I couldn’t see elasticsearch in there at all.

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
May 15, 2014

@Jonny: How did you install elasticsearch? There should be an init script installed at /etc/init.d/elasticsearch As for an elasticsearch monit file, they have a template in their Chef cookbook that you could adapt: https://github.com/elasticsearch/cookbook-elasticsearch/blob/master/templates/default/elasticsearch.monitrc.conf.erb

@Andrew SB I sued the debian package from elasticsearch in conjunction from your article.

There is an elasticsearch file in the /etc/init.d/ directory yes.The contents say it’s the startup script

“# /etc/init.d/elasticsearch – startup script for Elasticsearch” and it has various other controls in there.

  1. I should could take the template and place it in my “/etc/monit/monitrc” file?

  2. Where would I need to adapt it? (Apologies for this question, it’s all new to me)

Sorry to double post,

Looking at your stuff about mysql and configuring that: you have

check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = “/etc/init.d/mysql start” stop program = “/etc/init.d/mysql stop”

Would mine be for elasticsearch be:

Wasn’t sure what to put here on the first line as /var/run/ doesn’t contain anything for elasticsearch Then:

start program = “/etc/init.d/elasticsearch restart” with timeout 60 seconds stop program = “/etc/init.d/elasticsearch stop”

I’ve found this in my /etc/init.d/elasticsearch

Define other required variables

PID_FILE=/var/run/$NAME.pid DAEMON=$ES_HOME/bin/elasticsearch

Andrew SB
DigitalOcean Employee
DigitalOcean Employee badge
May 15, 2014

@Jonny: You can add it to the end of “/etc/monit/monitrc” file or you can create a new file called “/etc/monit/conf.d/elasticsearch”

To start, you could use this:

<pre> check process elasticsearch with pidfile /var/run/elasticsearch.pid start program = “/etc/init.d/elasticsearch restart” with timeout 60 seconds stop program = “/etc/init.d/elasticsearch stop” if cpu > 90% for 15 cycles then alert if totalmem > 90% for 15 cycles then alert if loadavg(15min) greater than 10 for 50 cycles then alert group elasticsearch </pre>

@Andrew SB Thank you for your help, that seems to have done it. It wasn’t running, but I added the above to my monitrc file and ran monit reload.

Thanks

Jonny

Hi Author,

Thanks for your great article. I would like to know how can I configure settings to get an email before restarting apache or mysql service?

My intention to get updated when service is down and this monit restarts it.

Please advise.

Many thanks

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
July 27, 2014

@mraheel83: See Setting an alert recipient.

You can add the following line to your Monit config file to get email alerts:

set alert your@email.address

I was looking at the Apache service, I get “Execution failed” I think the path should be “/var/run/apache2/apache2.pid” and not “/run/apache2.pid”

I am using Ubuntu 14.04

hi, need help with problem I have with monit. I have changed the default SSH listening port and since than monit keep sending me this message every 15 minutes: “‘sshd’ failed, cannot open a connection to INET[127.0.0.1:22] via TCP” What should I do to make monit stop doing that? Thanks!

Problem solved. I had to change default port in the file /usr/share/monit-cn/sshd.conf and then run update-monit.d. That did the work and now monit doesn’t send. me the message anymore.

Followed instructions and works perfectly. Thank you very much and boy what a useful service Monit is.

check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = “/etc/init.d/mysql start” stop program = “/etc/init.d/mysql stop”

should be

check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = “/sbin/mysql start” stop program = “/sbin/mysql stop”

Sorry i ment

check process mysqld with pidfile /var/run/mysqld/mysqld.pid start program = “/sbin/start mysql” stop program = “/sbin/stop mysql”

I got “execution failed” for apache error ??

What m i doing wrong? something near

    start program = "/etc/init.d/apache2 start" with timeout 60 seconds
    stop program  = "/etc/init.d/apache2 stop"```
Thanks

I corrected the path to pid file as /var/run/apache2/apache2.pid to start it working

so one extra directory before the pid file location, and /var/ at the starting. Although run is a symmlink in /var/ i had to give the location including /var/ as above and then only it started workig. Will look for explanation if someone know more Thanks :-)

Thanks for this. Everything that I’ve looked for regarding servers, I have found here. Keep up the good work.

I followed this exactly and got execution failed for apache on ubuntu 14.04

There’s a typo in the apache config:

check process apache with pidfile /run/apache2.pid

should be

check process apache with pidfile /var/run/apache2/apache2.pid

Can anyone help?

I’ve been unable to get monit to work with Mysql on my DO Ubuntu setup… Tried everything I can think of to no avail, as noted here: monit-process-mysql-not-monitored

I get the following error on apt-get:

Unpacking replacement dovecot-imapd ...
doveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
[....] Starting IMAP/POP3 mail server: dovecotdoveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
 failed!
invoke-rc.d: initscript dovecot, action "start" failed.
dpkg: warning: subprocess old post-removal script returned error exit status 1
dpkg: trying script from the new package instead ...
doveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
[....] Starting IMAP/POP3 mail server: dovecotdoveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
 failed!
invoke-rc.d: initscript dovecot, action "start" failed.
dpkg: error processing /var/cache/apt/archives/dovecot-imapd_1%3a2.1.7-7+deb7u1_amd64.deb (--unpack):
 subprocess new post-removal script returned error exit status 1
doveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
[....] Starting IMAP/POP3 mail server: dovecotdoveconf: Fatal: Error in configuration file /etc/dovecot/dovecot.conf line 7: Unexpected '}'
 failed!
invoke-rc.d: initscript dovecot, action "start" failed.
dpkg: error while cleaning up:
 subprocess new post-removal script returned error exit status 1
Errors were encountered while processing:
 /var/cache/apt/archives/dovecot-imapd_1%3a2.1.7-7+deb7u1_amd64.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

any ideas? :S

How to config for Apache and Varnish:

check process apache with pidfile /var/run/apache2/apache2.pid
    start program = "/etc/init.d/apache2 start" with timeout 60 seconds
    stop program  = "/etc/init.d/apache2 stop"

# Check varnish on port 80
check process varnish with pidfile /var/run/varnishd.pid
  start program = "/etc/init.d/varnish start"
  stop program = "/etc/init.d/varnish stop"
  if failed host <your droplet IP or Domain> port 80 protocol http
  and request "/blank.html"
  then restart
# Uncomment the following line to cause Monit to stop trying
# to bring Varnish back up if it keeps going down.
# if 3 restarts within 5 cycles then timeout

“We can add a small bit of configuration to Varnish to provide a URL for Monit to check that will be handled entirely in Varnish”. See https://www.computerminds.co.uk/articles/monitoring-varnish

Hi,

This is really awesome article. I had change what you are said here. I have a doubt regarding monit web interface. Is there any web interface for access monit performance? Is it possible to access monit in web instead of putty? Am looking forward your reply. Thanks.

In order to get Apache monitored, I needed to use:

check process apache with pidfile /run/apache2/apache2.pid

Also, if you plan on using the web version, you may need to open up your desired port in UFW (or similar).

I’ve following this article but I’m getting an “Execution failed” in apache too, whats the correct command line to put in monit config? apache2? thanks!

How to make sure that monit process automatically restarts after system restart on Ubuntu 14.0.4

Should this:

use address 12.34.56.789  # only accept connection from localhost

be this:

use address 127.0.0.1  # only accept connection from localhost

?

I’m trying to configure monit to alert if the filesystem use is above a certain percentage. As per the docs it says to use the following:

check filesystem <unique name> path <path>
    if SPACE usage > 80% then alert

Can someone please let me know what I use for <unique name> and <path>. Ive tried many things for example:

check filesystem rootfs path /

But everytime i run sudo monit start all, I get the error There is no service named "rootfs"

I am on Ubuntu 16.04

Thanks for your help

Let’s Tweak Your Online Business with the Best Press Release Distribution Service

http://shgseo.com/press-release-service/

I have installed Monit on Ubuntu server and that works fine. Now i want to install Monit on another Ubuntu server but that status also required to Show on same web interface (like nagios). Is Monit support on multiple servers? How can i configure ?

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.