1. 程式人生 > >自動重啟mysql

自動重啟mysql

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