Skip to content

Instantly share code, notes, and snippets.

@craighooghiem
Forked from tessro/kill-slow-queries.sh
Created November 22, 2017 19:35
Show Gist options
  • Save craighooghiem/bc8eb41551955a65472060d7692c5424 to your computer and use it in GitHub Desktop.
Save craighooghiem/bc8eb41551955a65472060d7692c5424 to your computer and use it in GitHub Desktop.
A script for killing slow MySQL queries, suited for a cron job
#!/bin/sh
# Credentials for a MySQL user with PROCESS, SUPER permissions
USERNAME=
PASSWORD=
# MySQL Server location
HOST=127.0.0.1
PORT=3306
TIMEOUT=60 # 1 minute
TARGET_USER= # MySQL user to monitor
MYSQL="mysql -u $USERNAME --password=$PASSWORD -h $HOST -P $PORT -B"
$MYSQL -N -e 'SHOW FULL PROCESSLIST' | cut -f 1,2,6 | awk '{ if ($3 > $TIMEOUT && $2 == $TARGET_USER) print "KILL " $1 ";" }'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment