Linux下Mysql连接或执行超时时,重启DB服务器

来源:互联网 发布:华为mate10知乎 编辑:程序博客网 时间:2024/06/18 09:00

#!/bin/shset -x TIMESTAMP=`date +%Y%m` LOG=/opt/batch/log/mysqlRestart_${TIMESTAMP}.log echo "Start execut at `date`." >>${LOG}# execute sql stat timeout()  {  waitfor=30command=$*  $command &  commandpid=$!  ( sleep $waitfor ; kill -9 $commandpid  > /dev/null 2>&1 && restartMysql ) &  watchdog=$!  sleeppid=$PPID  wait $commandpid > /dev/null 2>&1  kill $sleeppid > /dev/null 2>&1  }  selectsql(){ cmd="select count(*) from USER"cnt=$(mysql -h192.168.1.40 -P3306 -uroot -p123456 -s -e "${cmd}") result=$?echo "${result}" >>${LOG}if [[ ${result} = 1 ]];then restartMysqlelse echo "${cnt}" >>${LOG}fi}restartMysql(){service mysqld restartecho "Mysql restart done." >>${LOG}}timeout selectsqlecho "End execut at `date`." >>${LOG}exit;

0 0