Most of modern web applications rely on backend services (such as database, cache, scheduler…) to function properly. So keeping these services always up is very important, especially when your system is running on a production environment. Besides being able to put a service up again when it suddenly crashes for some reason, a monitor program should inform you about the incident right away. There’s an open source utility which can handle these tasks quite well – it is Monit.

1. Installation

The installation process is straightforward for both Ubuntu/Debian and RHEL/Centos systems:

Start monit:

2. Configuration

For Ubuntu/Debian, the main configuration file is /etc/monit/monitrc. For RHEL/Centos, it is /etc/monit.conf. Here are some default lines you may want to change (uncomment the lines to enable them):

This tells monit to check services at 2-minute intervals. I often set this to 30 or 45 seconds.

Use a mailserver so that monit can send alert emails to you when something happens. If you use ‘localhost’, make sure you have installed a mail daemon (sendmail, postfix,…) on your server.

Change this to your email address which monit will send alerts to

Monit comes with a built-in web server. It provides you with a ‘dashboard’ so that you can have a look at the status of the services, stop or start them as you want. Replace localhost with your server IP address in the line use address localhost and your machine IP in the line allow localhost. Change the default username and password for better security.

When you’ve finished configuring, remember to reload monit configuration:

After this step, you can access monit’s web interface via the url http://yourserverip:2812. But we’re not done yet, there’s more to do to monitor the services.

3. Services monitoring

At the end of the main configuration file, there’s a line that look like this: include /etc/monit.d/*.conf (on RHEL/Centos) or include /etc/monit/conf.d/* (on Ubuntu/Debian). This tells monit to load other configurations from the config folder (/etc/monit.d/ or /etc/monit/conf.d/). We’ll put our service configuration files in this folder. Here are the examples I use for common services for a Rails app:

The init script for puma looks like this:

The init script for sidekiq looks like this:

Once you’ve finished the configurations, check monit syntax:

If everything is all right, start monitoring all the services:

Or you can stop/start the services individually:

With servicename is defined in the service configuration file, preceded by check process

Nguồn: Blog Minh Danh

SSS Full-stack Engineer

Love Silicon Straits and want to know more about our company culture, working environment or job vacancies?
Find out more at

Silicon Straits
Be Challenged. Be Inspired. Be Different.

Published by Minh Danh

Developer and sysadmin at Silicon Straits Saigon