Created
September 10, 2015 15:19
-
-
Save matheus-santos/2f0262d408f278669a08 to your computer and use it in GitHub Desktop.
Script that kill slow processes running into Mysql DB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Author: Matheus Cesário <mts.cesario@gmail.com> | |
# Description: Killing all slow queries from bd | |
# Example: sh kill_them_all.sh -s 100 | |
# Variables | |
SECONDS=0 # Seconds | |
USER="" # DB user | |
PASSWORD="" # DB password | |
HOST="" # DB host | |
SSH_HOST="" # SSH host (for remote killing) | |
# Methods | |
# Killing all slow processes | |
killThemAll() | |
{ | |
# Getting slow processes | |
PROCESSLIST="mysql --skip-column-name -e \"SELECT CONCAT('KILL ', id, ';') AS run_this FROM INFORMATION_SCHEMA.PROCESSLIST WHERE user = '$USER' AND command = 'Query' AND info IS NOT NULL AND time > $SECONDS ORDER BY time DESC;\" --user=$USER --password=$PASSWORD --host=$HOST yata > /tmp/kill_them_all.sql" | |
# Killing them | |
KILL_THEM_ALL="mysql --user=$USER --password=$PASSWORD --host=$HOST yata < /tmp/kill_them_all.sql" | |
# Executing via SSH | |
ssh $SSH_HOST "$PROCESSLIST; $KILL_THEM_ALL;" | |
} | |
# Arguments | |
while getopts "hs:" OPTION | |
do | |
case $OPTION in | |
s) | |
SECONDS=$OPTARG | |
;; | |
?) | |
echo "Error: Argument Seconds (-s) is not defined." | |
exit 1 | |
;; | |
esac | |
done | |
# Executing only if time was defined | |
if [ ! -z $SECONDS ] | |
then | |
killThemAll # Executing method | |
exit 0 | |
else | |
echo "Error: Argument Seconds (-s) is not defined." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment