Ubuntu auto restart cron for Apache and Mysql after crash

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn


Websites never sleep but you may want to, this script lets you do that without having to worry about Apache or Mysql crashing randomly.

Obviously if you have reoccurring issues its best to check through the log files and fix any issues but this will help reduce down time on vital software crashes to less than a minute by checking for  Apache and Mysql services running and if none are found they get restarted.

Save this into a .sh file somewhere on your server and run this every minute usinng the built in Ubuntu Cron. If you are using a different OS and this doesn’t work replace the restart commands.

 

#!/bin/bash

# APACHE SECTION
RESTART="/etc/init.d/apache2 restart"
PGREP="/usr/bin/pgrep"
HTTPD="apache"
$PGREP ${HTTPD}
if [ $? -ne 0 ]
then
$RESTART
fi

# MYSQL SECTION
RESTARTM="/etc/init.d/mysql restart"
MYSQLD="mysqld"
$PGREP ${MYSQLD}
if [ $? -ne 0 ]
then
$RESTARTM
fi

 

To improve this script you could add email notifications and other vital services like sendmail which if they go down may be less apparent.

 


Posted by Adi on December 8, 2012

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *