I have problems where my website would go down during a 3 day weekend or even right when I go to sleep. I unfortunately did not have monitors setup so I relied on visitors and free services to contact me to fix the issue.
Usually restarting the database or rebooting the system fixes these issues. Wish I can get to the root cause of why mySQL acts this way, but for now, here is a simple method to automate the check on your website and restarting mySQL.
I've read a few articles where websites owners are just restarting the mySQL service every 2 hours or so, that's not ideal at all. That means every 2 hours, your website has a moment that shows an error to visitors.
The Automated Solution
Simple, just check your website for a text that should always show up. If it does not show up, reboot your server or restart mySQL.
1. You need root access (so you can restart mysql and run a cron job).
2. SSH into your server as root
3. Create a file (monitor.sh) with your favorite editor with the following
checkPage="$(wget -qO- http://www.spoofee.com | grep 50744)"
if [ "$checkPage" == "" ]; then
echo "Page is not loading, restarting mysql"
/etc/init.d/mysql restart
SUBJECT="mysql restarted"
EMAIL="whoever@gmail.com"
date +"%m-%d-%Y %T" | /bin/mail -s "$SUBJECT" "$EMAIL"
else
echo "All ok";
fi
replace the red items with what's applicable to your site. 50744 is the text I'm looking for on my website to show that the page has loaded successfully. Of course the email is optional but you would like to know how often and when these are occurring right?
4. Make sure it's executable by doing something like
chmod 700 /home/user/scripts/monitor.sh
5. Create a cronjob to run this script every X mins. Type
crontab -e
6. Enter the following to your cronjob. My example runs the script every 15 minutes.
*/15 * * * * /home/user/scripts/monitor.sh
No comments:
Post a Comment