网站常用自动重启服务脚本

17-08-04 09:28 字数 2427 阅读 5075 已编辑
1.监控mysql,负载大于20就自动重启mysql数据库,可放在crontab执行
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
dt=`date +%F-%H:%M:%S`
load=`uptime |awk '{print $10}'|awk -F"." '{print $1}'`
dk=`netstat -tunpl | grep mysql | awk '{print $4}' | awk -F: '{print $2}'`
if [ "$load" -gt 20 ]
then
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' >> /shell/mysql.txt
echo "system load is $load" >> /shell/mysql.txt
echo "$dt restart mysql" >> /shell/mysql.txt
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' >> /shell/mysql-sql.txt
echo "$dt sql processlist " >> /shell/mysql-sql.txt
#记录mysql运行的process
mysql -uroot -p数据库密码 -B -e "show full processlist;" >> /shell/mysql-sql.txt
/etc/init.d/mysql restart > /dev/null
else 
 echo 'system load is ok !!!'
fi
if [ $dk -eq 3306 ];then
echo "mysql is running!!"
else
/etc/init.d/mysql restart > /dev/null
fi

2.监控apache2,负载大于20,重启apache
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
dt=`date +%F-%H:%M:%S`
ip=`ifconfig eth0 |grep "inet addr"| cut -f 2 -d ":"|cut -f 1 -d " "`
load=`top -b -n 1 | awk -F "[ :]+" 'NR==1{print $15}' | sed 's/,//'`
dk=`netstat -tunpl | grep apache2 | grep :80 |awk '{print $4}' |awk -F ':' '{print $2}'`
if [ `echo "$load > 20" | bc` -eq 1 ];then
 echo system load is $load !!!
echo '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++' >> /shell/apache2-$ip.txt
/etc/init.d/apache2 restart > /dev/null
echo "$dt restart apache2">> /shell/apache2-$ip.txt
else 
 echo system load is $load ok !!!
fi

if [ $dk -eq 80 ];then
echo "apache2 is running!!"
else
/etc/init.d/apache2 restart > /dev/null
fi

3.监控服务器线程数,大于1500,重启apache
#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
centos_status(){
    for i in {1..10}
    do
        /etc/init.d/httpd restart &> /dev/null
        apache_status=`netstat -antp |grep -c httpd`
        if [ $apache_status -ge 1 ]
        then
            echo `date +%F_%H:%M:%S` >> restart.log
            break
        else
            continue
        fi
    done

}
ubuntu_status(){
        for i in {1..10}
        do
            /etc/init.d/apache2 restart > /dev/null
            apache_status=`netstat -antp |grep -c apache`
            if [ $apache_status -ge 1 ]
            then
                echo `date +%F_%H:%M:%S` >> restart.log
                break
            else
                continue
            fi
        done
        }
process(){
    system=`cat /etc/issue |head -1 |awk '{print $1}'`
    if [ $system = Ubuntu ]
    then
        process=`top -b -n 1 |grep 'Tasks' |awk -F "[: ]+" '{print $2}'`
        if [ $process -ge 1500 ]
        then
            ubuntu_status
            echo 'process more than 1500,restart apache success!!' >> restart.log
            echo -e "nn" >> restart.log
        fi
    elif [ $system = CentOS ]
    then
        process=`top -b -n 1 |grep 'Tasks' |awk -F "[: ]+" '{print $2}'`
        if [ $process -ge 1500 ]
        then
            centos_status
            echo 'process more than 1500,restart apache success!!' >> restart.log
            echo  -e "nn" >> restart.log
        fi
    fi
}
process
0人点赞>
关注 收藏 改进 举报
3 条评论
排序方式 时间 投票
Up骚年

太6了,大牛?。感谢分享。

fengzy
请登录后发表评论
静坐常思己过,闲谈莫记他非!
文章
2
粉丝
1
喜欢
0
收藏
6
排名 : 20
访问 : 1.06万
私信