sh方式是:
#!/bin/sh
pidof mysqld >/dev/null
if [ $? -eq 0 ]
then
echo "It is running."
else
echo "At `date` MySQL Server was stopped">> /home/mysql_log
service mysql start
fi
然后crontab -e添加:
*/2 * * * * sh /home/sh/check_mysql.sh
下面是python,但不一定好使:
vi /home/sh/check_mysql.py
import os
import time
import datetime
output = os.popen("service mysql status").read()
print( time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime( time.time() ) ) )
print( output )
if "not running" in output or output=="":
print( "restart")
output = os.popen("service mysql restart").read();
print( output )
else:
print( "ok" )
然后crontab -e添加:
*/2 * * * * sudo python /home/sh/check_mysql.py

本文介绍了一种使用Shell脚本和Python脚本来监控MySQL服务运行状态的方法。通过定时任务检查MySQL服务是否正常运行,如果发现服务停止,则自动重启MySQL服务,并记录相关信息。
254

被折叠的 条评论
为什么被折叠?



