Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DavidGoodwin/52e9f93725d5c6181f23300226f4e6bb to your computer and use it in GitHub Desktop.
Save DavidGoodwin/52e9f93725d5c6181f23300226f4e6bb to your computer and use it in GitHub Desktop.
Kill long running MySQL queries (in AWS RDS)
#!/bin/bash
MYSQL="mysql --defaults-extra-file=/path/to/something.cnf"
# Find queries which have been running for longer than 1800 seconds.
for PID in $($MYSQL -BNe "SELECT id FROM information_schema.processlist WHERE command <> 'Sleep' AND info NOT LIKE '%PROCESSLIST%' AND command <> 'Killed' AND time > 1800" )
do
#OUTPUT=$($MYSQL -NBe "SELECT id,time,command,info FROM information_schema.processlist WHERE id = $PID ")
#echo "Long running query output - $OUTPUT "
# In AWS RDS :
$MYSQL -e "call mysql.rds_kill($PID)"
# For normal MySQL :
# $MYSQL -e "kill $PID"
done
@seandelaney
Copy link

Thank you!

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