Check whether mysql is successfully started (bat + bash), mysqlbash
Many of my friends prefer windows. For more information, see the following code.
Method 1: Skip
@echo off for /f "usebackq" %%i in (`"tasklist|find /c "mysqld.exe""`) do ( set chkstat=%%i ) if %chkstat% == 0 ( net start mysqlecho ================ >> %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 )
Method 2:
@ Echo offfor/f "skip = 3 tokens = 4" % I in ('SC query mysql') do set "zt = % I" & goto: next: nextif/I "% zt %" = "RUNNING" (echo has found that the service is RUNNING and is shutting down the service net stop mysql) else (echo this service is now processing stopped, enabling the Service net start mysql) exitpause
Use shell commands in linux
Checks whether MySQL is down. If it is down, it is started.
If your MySQL database goes down frequently, you can use this script to automatically start it after it goes down and add it to the crontab for execution.
See http://www.codeproject.com/Articles/988967/Mysql-Uptime-Check-Script for more information
#!/bin/bash result=`/usr/bin/mysqladmin ping`expected='mysqld is alive' if [[ "$result" != "$expected" ]]thenecho "It's dead - restart mysql" # email subjectSUBJECT="[MYSQL ERROR] - Attempting to restart service" # Email To ?EMAIL="info@endyourif.com" # Email text/messageEMAILMESSAGE="/tmp/emailmessage.txt"echo "$result was received"> $EMAILMESSAGEecho "when we were expected $expected" >>$EMAILMESSAGE# send an email using /bin/mailmail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE sudo /etc/init.d/mysql restartfi
Method 2,
#!/bin/bash#mysql checkPORT="0"PORT=`netstat -lnt | grep 3306 | wc -l `echo $PORTif [ $PORT -eq 1 ] thenecho "mysql is running"elseecho "mysql is not running"echo "progrome reeady to start mysql "sudo service mysql start./check_mysql.shfi