当它们发生故障时重启Apache和MySQL

时间:2015-08-10 23:57:17

标签: mysql linux bash apache

如何编写bash脚本,以便当我的Apache服务器或MySQL发生故障时,它们会自动重启?

现在,我不得不在它们发生故障时手动执行这些命令:

sudo service apache2 restart
sudo /etc/init.d/mysql restart

要检查我的网站并重新启动服务器很烦人,如果这两台服务器都在运行,我是否可以使用bash脚本每隔5分钟检查一次?

1 个答案:

答案 0 :(得分:0)

#!/bin/bash
PATH=/usr/sbin:/usr/bin:/sbin:/bin
if [[ ! "$(/usr/sbin/service mysql status)" =~ "active (running)" ]]
then
    echo "MySQL restarted" | mail -s "yourwebsite server mysql restarted" yourmail@gmail.com
    sudo service mysql start
fi

if [[ ! "$(/usr/sbin/service apache2 status)" =~ "active (running)" ]]
then
    echo "Apache restarted" | mail -s "yourwebsite server Apache restarted" yourmail@gmail.com
    sudo service apache2 start
fi

在bash上方另存为mysqlfix.sh

获得适当的许可

chmod +x mysqlfix.sh

添加cronjob

crontab -e

*/1 * * * * /root/mysqlfix.sh