Script to check if Website alive and restart Services if necessary

I was looking into a LAMP Server these days and had one effect that some pages became unresponsive over time. While the Error was not easy to detect and fix we decided to create a check script to check a certain URL and in case of a timeout server services would be restarted automatically. Not the bast possible way but maybe the most straight forward one.

Please adjust the service commands on the top to have the right restart commands inside.

#! /usr/bin/bash
function restart {
service nginx restart
service mariadb restart
service httpd restart
}

function check {
if [ $? -ne 0 ] ; then
echo "Error occurred getting URL $1:"
if [ $? -eq 6 ]; then
echo "Unable to resolve host"
fi
if [ $? -eq 7 ]; then
echo "Unable to connect to host"
restart;
fi
restart;
exit 1
fi

}

Call for this script looks like…

/scripts/check.sh http://www.URLTOCHECK.io

Cron like

*/15 * * * * /scripts/check.sh http://www.URLTOCHECK.io

MYSQL Long kill long Querys via Script in Cron

As part of my private life i sometimes help friends with their Webservers. Recently i had a case of a LAMP Server including Maria / MySQL DB. Some Queries lasted “forever” in the DB and consumed a lot of CPU. I created small script which looks into the DB and kills thoos querys.

This Script was done for a Server where the Password of MySQL is stored in a File (Plesk) Therefore the -p Command contains a cat of the password. Please adjust the script to take the right username and password.


#!/bin/sh
# Kill long Mysql Queries
mysql -uadmin -p`cat /etc/psa/.psa.shadow` -ANe"SELECT id FROM information_schema.processlist WHERE time > 360;" | cut -d: -f2 |\
while read id
do
mysql -uadmin -p`cat /etc/psa/.psa.shadow` -ANe"kill $id;"
done

Cron looks like this to check this all 10 minutes and not receive any notification about it.

*/10 * * * * /root/killlongdbqueries.sh >/dev/null 2>&1