Recently, I have just entered a new company, and took over a site here, because the site's architectural design is not reasonable, resulting in the pressure of MySQL is always very large, often the locked process of time out, so wrote a section of Linux shell script to kill these processes regularly.
The script is as follows:
Copy Code code as follows:
#!/bin/bash
Mysql_pwd= "XXXXXX" #mysql的root密码
Mysql_exec= "/usr/local/mysql/bin/mysql"
tmp_dir= "/tmp"
File_sh= "$tmp _dir/mysql_kill_locked.sh"
file_tmp= "$tmp _dir/mysql_kill_locked.tmp"
file_log= "$tmp _dir/mysql_kill_locked.log" #日志
$mysql _exec-uroot-p$mysql_pwd-e "Show Processlist" | Grep-i "Locked" > $file _tmp
Cat $file _tmp >> $file _log
For line in ' Cat $file _tmp | awk ' {print '} '
Todo
echo "$mysql _exec-uroot-p$mysql_pwd-e" Kill $line "" >> $file _sh
Done
chmod +x $file _sh
SH $file _sh #执行临时脚本
> $file _sh #清空临时脚本
Finally, the script is added to the crontab, and can be executed regularly.