自动重启mysql

来源:互联网 发布:html5权威指南 源码 编辑:程序博客网 时间:2024/05/01 05:25

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