Skip to content

Instantly share code, notes, and snippets.

@tperalta82
Created September 21, 2016 16:16
Show Gist options
  • Save tperalta82/02ae75c4979fc47c9fe25160b558735a to your computer and use it in GitHub Desktop.
Save tperalta82/02ae75c4979fc47c9fe25160b558735a to your computer and use it in GitHub Desktop.
Simple Mysql check/restart
#!/bin/bash
MSCONN=$(echo "SHOW GLOBAL STATUS LIKE 'Uptime';" | mysql -pyoursupersecurepasswordthatnotevengodknows)
CONSTAT=$?
SSTAT=$(service mysqld status)
LRET=$?
if [ $CONSTAT -ne 0 ] || [ $LRET -ne 0 ]; then
subject="Mysql Server Down / Unresponsive";
recipients="[email protected],thisoneisnot@nopecom"
message="Mysql Service is down or not responding, restarting service, if it fails to restart or if you receive this message again in 1 minut$
"$(date)
/usr/sbin/sendmail "$recipients" << EOF
subject:$subject
from:$from
$message
EOF
/sbin/service mysqld restart
echo "ERR"
fi
@abacao
Copy link

abacao commented Sep 21, 2016

Should the variables be at the top of the script to be easier to fiddle with without messing the rest of the code?
You are connecting to a local mysql right? Maye you could extend the script to a larger game if you introduce the mysql server IP in the configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment