Monitoring services on your server is crucial for ensuring reliability and rapid recovery from downtime.
Monit is a simple, lightweight tool perfect for monitoring and managing services automatically.
I personally use it and adjust this general config file to monitor the service i want per server, is lite weight and works with enhance.
How to Setup
Step 1: Install Monit
For Debian/Ubuntu:
sudo apt update
sudo apt install monit
Step 2: Basic Monit Configuration
Edit Monit's config file:
sudo nano /etc/monit/monitrc
Enable the web interface (optional):
set httpd port 2812 and
use address 0.0.0.0
allow admin:monit
Step 3: Configuring Monit to Monitor Essential Services
Add the following to /etc/monit/monitrc:
#SSH
check program sshd with path "/bin/systemctl is-active --quiet ssh"
if status != 0 then exec "/bin/systemctl restart ssh"
if 5 restarts within 5 cycles then timeout
#Cron
check program cron with path "/bin/systemctl is-active --quiet cron"
if status != 0 then exec "/bin/systemctl restart cron"
if 5 restarts within 5 cycles then timeout
#Rspamd
check program rspamd with path "/bin/systemctl is-active --quiet rspamd"
if status != 0 then exec "/bin/systemctl restart rspamd"
if 5 restarts within 5 cycles then timeout
#Postfix
check program postfix with path "/bin/systemctl is-active --quiet postfix"
if status != 0 then exec "/bin/systemctl restart postfix"
if 5 restarts within 5 cycles then timeout
#Dovecot
check program dovecot with path "/bin/systemctl is-active --quiet dovecot"
if status != 0 then exec "/bin/systemctl restart dovecot"
if 5 restarts within 5 cycles then timeout
#Pure-FTP
check program pure-ftpd with path "/bin/systemctl is-active --quiet pure-ftpd"
if status != 0 then exec "/bin/systemctl restart pure-ftpd"
if 5 restarts within 5 cycles then timeout
#LiteSpeed Web Server (LSWS)
check program lsws with path "/bin/systemctl is-active --quiet lsws"
if status != 0 then exec "/bin/systemctl restart lsws"
if 5 restarts within 5 cycles then timeout
#Nginx or Apache (choose one and uncomment)
# Nginx
#check program nginx with path "/bin/systemctl is-active --quiet nginx"
# if status != 0 then exec "/bin/systemctl restart nginx"
# if 5 restarts within 5 cycles then timeout
# Apache
#check program apache2 with path "/bin/systemctl is-active --quiet apache2"
# if status != 0 then exec "/bin/systemctl restart apache2"
# if 5 restarts within 5 cycles then timeout
#Server load
check system localhost
if loadavg (1min) > 8 then alert
Step 5: Verify Status
Check status easily with:
sudo monit status
Alerts
Set up email alerts in your monitrc to receive notifications for service restarts and failures:
set mailserver smtp.example.com port 587
username "user@example.com" password "yourpassword"
using tls
set alert admin@example.com
Additional Examples
# Alert on high RAM usage
#check system localhost
# if memory usage > 90% then alert
# Alert on disk usage
#check filesystem rootfs with path /
# if space usage > 80% then alert
# Alert on inode usage
#check filesystem rootfs with path /
# if inode usage > 90% then alert
Now Monit will automatically monitor, restart services as needed, and alert you via email if anything goes wrong, ensuring your server remains stable and reliable