快捷搜索:   nginx

定时重启apache,MYSQL以及整个服务器并发短信通知的脚本

这个脚本是在https://www.bnxb.com/shell/27321.html 的基础上修改,可以自动判断时间,每逢周五自动重启服务器,其他时间的每天半夜1:10只对apache和mysql进行重启,使用139邮箱收信,可获得免费短信提醒。

vi reboot.sh 将下面脚本加入


#!/bin/sh
datetime=`date +%w`
if [ $datetime -eq 5 ]
then
echo "server is turn off and reboot" |mutt -s "Server reboot" xxx@139.com
/sbin/reboot
sleep 30
else
if [ `pgrep httpd | wc -l` -gt 0 ]
then
/usr/sbin/apachectl stop
sleep 5
pkill httpd
sleep 3
/etc/init.d/mysqld stop
sleep 8
/etc/init.d/mysqld start
sleep 3
/usr/sbin/apachectl start
sleep 8
if [ `pgrep httpd | wc -l` -eq 0 ]
then
echo "Apache is down at:" $(date +"%y-%m-%d %H:%M:%S") |mutt -s "Apache is down,Please try to restart it!" xxx@139.com
fi
else
sleep 3
/usr/sbin/apachectl start
if [ `pgrep mysqld | wc -l` -eq 0 ]
then
/etc/init.d/mysqld start
sleep 8
if [ `pgrep mysqld | wc -l` -eq 0 ]
then
echo "Mysql is down at:" $(date +"%y-%m-%d %H:%M:%S") |mutt -s "Mysql is down,Please try to restart it!" xxx@139.com
fi
fi
fi
fi


chmod +x reboot.sh
crontab -e
设置一下在每天半夜1:10执行
10 1 * * * /root/reboot.sh

service crontal restart 一下,使配置生效

另外稍稍修改下,这个脚本还可以改成在自动重启apache失败或者mysql失败后给您发送电子邮件通知


#!/bin/sh
datetime=`date +%w`
/usr/sbin/apachectl stop
sleep 5
pkill httpd
/usr/sbin/mysqld stop
sleep 8
if [ $datetime -eq 5 ]
then
echo "server reboot" |mutt -s "Server reboot" xxx@139.com
/sbin/reboot
else
/usr/sbin/mysqld start
sleep 5
/usr/sbin/apachectl start
echo "Web server restart" |mutt -s "Apache Mysql restart" xxx@139.com
fi
sleep 8
result=`pidof httpd`
if [ "$result" == "" ];then
   echo $(date +"%y-%m-%d %H:%M:%S") "apache fail to start" | mutt -s "Apache fail to start" xxx@139.com
fi
sleep 3
result2=`pidof mysqld`
if [ "$result2" == "" ];then
   echo $(date +"%y-%m-%d %H:%M:%S") "mysql fail to start" | mutt -s "mysql fail to start" xxx@139.com
fi 



139邮箱的设置

blob.png

顶(4)
踩(0)

您可能还会对下面的文章感兴趣:

最新评论