How to Terminate Sleeping MySQL Processes with a Linux Bash Script

Here’s an example of a Linux bash script that checks for sleeping MySQL processes and terminates them:


# MySQL connection details

# Get the list of sleeping processes from MySQL
PROCESS_LIST=$(mysql -u $DB_USER -p$DB_PASS -Bse "SELECT ID FROM information_schema.processlist WHERE COMMAND = 'Sleep';")

# Iterate over the sleeping processes and kill them
    mysql -u $DB_USER -p$DB_PASS -e "KILL $PROCESS_ID;"

echo "Sleeping processes terminated."

Make sure to replace "your_username" and "your_password" with your actual MySQL username and password. The script uses the mysql command-line tool to connect to MySQL and retrieve the list of sleeping processes. Then, it iterates over the process list and terminates each sleeping process with the KILL command.

Save the above script into a file, e.g., Make the script executable by running chmod +x Finally, execute the script using ./ It will terminate all sleeping MySQL processes.

Please exercise caution when terminating processes. It is recommended to test scripts on non-production environments first and ensure you have appropriate permissions to execute such actions on your MySQL server.