You can use monit to easily restart services as apache2, mysql, nginx and others. To install and setup is very easy, follow this lines:
# Install monit using apt apt-get install monit #edit monitrc to configure httpd, # this is the base configuration file of monit and you can # configure other things like check interval of monit vim /etc/monit/monitrc # find and uncomment httpd service: set httpd port 2812 and use address localhost # only accept connection from localhost allow localhost # allow localhost to connect to the server and allow admin:monit # require user 'admin' with password 'monit'
Save the file, and now you need to configure de apache and mysql file that will be use to monitoring these services, if you are user 16.04 Ubuntu version or above, you already have this templates in conf-available folder, just copy to conf-enabled folder, if you don’t have, you can see the file that you need create in conf.d folder in the end of this post.
cp/etc/monit/conf-available/apache2 /etc/monit/conf-enabled/apache2 cp/etc/monit/conf-available/mysql /etc/monit/conf-enabled/mysql # reload monit monit reload # start new services monit start all # ready!
Older Ubuntu versions:
If you don’t have conf-available folder, you can create the files of services that you want to monitoring, inside conf.d folder.
sudo vim /etc/monit/conf.d/apache2 #write and save: check process apache with pidfile /var/run/apache2/apache2.pid group www group apache start program = "/etc/init.d/apache2 start" stop program = "/etc/init.d/apache2 stop" if 4 restarts within 20 cycles then timeout if failed host localhost port 80 with protocol http and request "/server-status" with timeout 25 seconds for 4 times within 5 cycles then restart depend apache_bin depend apache_rc check file apache_bin with path /usr/sbin/apache2 group apache include /etc/monit/templates/rootbin check file apache_rc with path /etc/init.d/apache2 group apache include /etc/monit/templates/rootbin
sudo vim /etc/monit/conf.d/mysql #write and save: check process mysqld with pidfile /var/run/mysqld/mysqld.pid group database group mysql start program = "/etc/init.d/mysql start" stop program = "/etc/init.d/mysql stop" if failed host localhost port 3306 protocol mysql with timeout 15 seconds for 3 times within 4 cycles then restart if failed unixsocket /var/run/mysqld/mysqld.sock protocol mysql for 3 times within 4 cycles then restart if 5 restarts with 5 cycles then timeout depend mysql_bin depend mysql_rc check file mysql_bin with path /usr/sbin/mysqld group mysql include /etc/monit/templates/rootbin check file mysql_rc with path /etc/init.d/mysql group mysql include /etc/monit/templates/rootbin