检查mysql是否成功启动的方法(bat+bash)
程序员文章站
2024-02-20 09:22:28
很多朋友更喜欢windows下面的可以参考下面的代码
第一种方法:也比较普遍的方法,包括rsync、serv_u等都可以,通过查找进程中的exe文件,例如mysql就是m...
很多朋友更喜欢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="info@endyourif.com" # 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
方法二、
#!/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