很多朋友更喜欢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)exitpause
linux系统中通过shell命令实现
检测 MySQL 是否宕掉,如果宕掉则启动
#!/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 restartfi
⽅法⼆、
#!/bin/bash#mysql checkPORT=\"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.shfi
因篇幅问题不能全部显示,请点此查看更多更全内容