Search This Blog

Tuesday, November 13, 2018

How to Set up monit to restart Apache and MySQL automatically in Centos

Install monit from EPEL, first install EPEL:

$ sudo rpm -Uvh  http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

$ sudo yum -y install monit

in /etc/monit.conf

# set daemon mode timeout to 1 minute
set daemon 60
# Include all files from /etc/monit.d/
include /etc/monit.d/*

check process mysql with pidfile /var/run/mysqld/mysqld.pid
   group database
   start program = "/etc/init.d/mysqld start"
   stop program = "/etc/init.d/mysqld stop"
   if failed host 127.0.0.1 port 3306 then restart
   if 5 restarts within 5 cycles then timeout
# monitoring apache

check process apache with pidfile /var/run/httpd/httpd.pid
   group www
   start program = "/etc/init.d/httpd start"
   stop program  = "/etc/init.d/httpd stop"

   if failed host www.i88.ca port 80 protocol http
       then restart

# the following is to check web site with content.

check host www.i88.ca with address www.i88.ca
if failed url http://www.i88.ca and content == "IT.I88.CA"
timeout 30 seconds for 2 cycles then exec "/usr/bin/python /root/restart-do.py"
Finally:

$ sudo  /etc/init.d/monit start