service mysqld status
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
echo "At time: `date` :MySQL is stop .">> /var/log/mysql_messages
service mysql start
#echo "At time: `date` :MySQL server is stop."
else
echo "MySQL server is running ."
fi
将上述脚本保存到mysql.sh中,上传到服务器,运行该脚本可以发现输出数据库服务正在运行
# sh mysql.sh
MySQL server is running
每隔一定时间自动运行脚本
crontab -e
输入(如果不能输入,按键盘上的Insert键就能输入了)
*/5 * * * * /your_dir/mysql.sh
service cron restart
#!/bin/bash
pgrep -x mysqld &> /dev/null
if [ $? -ne 0 ]
then
mosquitto_pub -t mysql_status -m "Failed"
service mysql start
#echo "At time: `date` :MySQL server is stop."
else
mosquitto_pub -t mysql_status -m "Running"
fi
import paho.mqtt.client as mqtt
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("mysql_status")
#订阅,第一个参数是订阅的主题
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("主机名或ip", 1883, 60)
#第一个参数为主机名,及Mosquitto所在服务器,第二个参数是端口
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()
原文:http://www.cnblogs.com/star91/p/5059378.html