A lot of friends like windows below can refer to the following code
The first method: Also more common methods, including rsync, Serv_u, etc. can be found in the process of EXE files, such as MySQL is 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
The second method:
@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 has discovered that the service is running and is shutting down service
net stop MySQL
) Else (
echo the service now handles the stop state and is now opening the service
net start MySQL
)
Exit
Pause
The Linux system is implemented through shell commands
Detect if MySQL is down, start if it's down
If your MySQL is often down, you can use this script to implement the automatic startup after the outage, add it to the crontab to execute it.
For more information please see 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 Serv Ice "
# 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 usi Ng/bin/mail
mail-s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
sudo/etc/init.d/mysql restart
fi
Method Two,
#!/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 "The MySQL is isn't running"
echo "Progrome reeady to start MySQL "
sudo service mysql start
./check_mysql.sh
fi