Here is the bash script I created for my server. In case anyone else comes across this thread and would like to use my script for checking mysql and restarting. This is for Ubuntu with Sentora, but I'm sure it will work for most linux distributions (you may need to make some changes)
I added this line in crontab -e to run this script every 10 minutes
*/10 * * * * /bin/sh /home/root/scripts/mysql-service-check.sh >/dev/null 2>&1
UP=$(pgrep mysql | wc -l)
RFILE="/home/root/scripts/mysql-service-check.txt"
FFILE="/home/root/scripts/mysql-failure-log.txt"
NOW=$(date)
LOAD=$(uptime)
if [ $UP -eq 0 ]
then
"MySQL is down"
sudo service mysql start
echo "$NOW" >> $FFILE
echo "Subject: SERVER ALERT - Mysql Server was Down" > $RFILE
echo "The mysql server on your server was found to be down by our monitoring script on $NOW. The site was down for a maximum of 10 minutes. Crash logged in mysql-failure-log.txt" >> $RFILE
echo "" >> $RFILE
echo "This email should be proof that the services have been restored and the site is up and running. However, you should also confirm by going here: www.yoursite.com." >> $RFILE
echo "" >> $RFILE
echo "- YOUR SCRIPT MONKEY" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "DIAGNOSTICS:" >> $RFILE
echo "$LOAD" >> $RFILE
echo "" >> $RFILE
echo "Syslog Error Log:" >> $RFILE
tail -100 /var/log/syslog | grep mysql >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "Mysql Error Log:" >> $RFILE
tail -100 /var/log/mysql/error.log >> $RFILE
/usr/sbin/sendmail you@youremail.com < $RFILE
fi
Hope this helps someone else. This script works perfectly to get things back up and running quickly and automatically, the most important thing. I would think Sentora should have something like this built in as mysql is critical and a down server means NO websites. But anyway, this script does the job. Now, I need to find out why mysql is crashing in the first place.
I added this line in crontab -e to run this script every 10 minutes
*/10 * * * * /bin/sh /home/root/scripts/mysql-service-check.sh >/dev/null 2>&1
UP=$(pgrep mysql | wc -l)
RFILE="/home/root/scripts/mysql-service-check.txt"
FFILE="/home/root/scripts/mysql-failure-log.txt"
NOW=$(date)
LOAD=$(uptime)
if [ $UP -eq 0 ]
then
"MySQL is down"
sudo service mysql start
echo "$NOW" >> $FFILE
echo "Subject: SERVER ALERT - Mysql Server was Down" > $RFILE
echo "The mysql server on your server was found to be down by our monitoring script on $NOW. The site was down for a maximum of 10 minutes. Crash logged in mysql-failure-log.txt" >> $RFILE
echo "" >> $RFILE
echo "This email should be proof that the services have been restored and the site is up and running. However, you should also confirm by going here: www.yoursite.com." >> $RFILE
echo "" >> $RFILE
echo "- YOUR SCRIPT MONKEY" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "DIAGNOSTICS:" >> $RFILE
echo "$LOAD" >> $RFILE
echo "" >> $RFILE
echo "Syslog Error Log:" >> $RFILE
tail -100 /var/log/syslog | grep mysql >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "" >> $RFILE
echo "Mysql Error Log:" >> $RFILE
tail -100 /var/log/mysql/error.log >> $RFILE
/usr/sbin/sendmail you@youremail.com < $RFILE
fi
Hope this helps someone else. This script works perfectly to get things back up and running quickly and automatically, the most important thing. I would think Sentora should have something like this built in as mysql is critical and a down server means NO websites. But anyway, this script does the job. Now, I need to find out why mysql is crashing in the first place.