放假前写的,监控服务器端的 MySQL 状态。
#!/bin/sh MUSER=MySQL User MPASS=MySQL Password MHOST=MySQL Host MSTART="/etc/init.d/mysqld start" MADMIN=/usr/bin/mysqladmin MTO=Administrator Email $MADMIN -h $MHOST -u $MUSER -p"${MPASS}" ping 2>/dev/null 1>/dev/null if [ $? -ne 0 ]; then echo "Server ERROR: MySQL Service Stopped. Restart Service..." $MSTART>/dev/null sleep 3 o=$(ps cax | grep -c ' mysqld$') if [ $o -eq 1 ]; then echo "MySQL Restart Success." else echo "MySQL Service Stopped." | mail -s "MySQL Server Crashed" $MTO fi else echo "MySQL is alive" fi
功能很简单,监控 MySQL 服务是否运行,如果挂了就尝试重启服务,重启失败就发邮件通知管理员。把这个 script 放到 crontabe 里就完事了。