Kill long sleeping connections

I have many servers that have applications that do not close connections properly or that use a connection pool. Sometimes I get a lot of sleeping connections that I want to kill. So here is a simple bash script you can use to kill the sleeping connections.

#!/bin/bash
user=<user name>
pass=<password>

for i in /tmp/*.sock ;
do
for j in $(mysql -u$user -p$pass -S $i  -e 'show processlist' | grep 'sleep' | awk '{print $1}' )
do
mysql -u$user -p$pass -S $i  -e "kill $j"
done
done
VN:F [1.9.3_1094]
Rating: 10.0/10 (1 vote cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)
Kill long sleeping connections, 10.0 out of 10 based on 1 rating
You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.

Twitter Delicious Facebook Digg Stumbleupon Favorites More
Designed by: MySQL How 2

Switch to our mobile site