檢討mysql能否勝利啟動的辦法(bat+bash)。本站提示廣大學習愛好者:(檢討mysql能否勝利啟動的辦法(bat+bash))文章只能為提供參考,不一定能成為您想要的結果。以下是檢討mysql能否勝利啟動的辦法(bat+bash)正文
許多同伙更愛好windows上面的可以參考上面的代碼
第一種辦法:也比擬廣泛的辦法,包含rsync、serv_u等都可以,經由過程查找過程中的exe文件,例如mysql就是mysqld.exe
@echo off for /f "usebackq" %%i in (`"tasklist|find /c "mysqld.exe""`) do ( set chkstat=%%i ) if %chkstat% == 0 ( net start mysql echo ================ >> %date:~0,10%.log echo %date% %time% check mysql service stop >> %date:~0,10%.log echo restart mysql service! >> %date:~0,10%.log echo ================ >> %date:~0,10%.log )
第二種辦法:
@echo off for /f "skip=3 tokens=4" %%i in ('sc query mysql') do set "zt=%%i" &goto :next :next if /i "%zt%"=="RUNNING" ( echo 曾經發明該辦事在運轉,正在曾經封閉辦事 net stop mysql ) else ( echo 該辦事如今處置停滯狀況,正在如今開啟辦事 net start mysql ) exit pause
linux體系中經由過程shell敕令完成
檢測 MySQL 能否宕失落,假如宕失落則啟動
假如你的 MySQL 常常宕機,可以用這個劇本來完成宕機後主動啟動,把它加到 crontab 裡履行便可。
具體引見請看 http://www.codeproject.com/Articles/988967/Mysql-Uptime-Check-Script
#!/bin/bash result=`/usr/bin/mysqladmin ping` expected='mysqld is alive' if [[ "$result" != "$expected" ]] then echo "It's dead - restart mysql" # email subject SUBJECT="[MYSQL ERROR] - Attempting to restart service" # Email To ? EMAIL="[email protected]" # Email text/message EMAILMESSAGE="/tmp/emailmessage.txt" echo "$result was received"> $EMAILMESSAGE echo "when we were expected $expected" >>$EMAILMESSAGE # send an email using /bin/mail mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE sudo /etc/init.d/mysql restart fi
辦法2、
#!/bin/bash #mysql check PORT="0" PORT=`netstat -lnt | grep 3306 | wc -l ` echo $PORT if [ $PORT -eq 1 ] then echo "mysql is running" else echo "mysql is not running" echo "progrome reeady to start mysql " sudo service mysql start ./check_mysql.sh fi