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

**URL:** <https://www.shiqidu.com/t/topic/202>\
**Category:** 开发调优\
**Tags:** Linux\
**Created:** [2017 年8 月 4 日 01:28 UTC](https://www.shiqidu.com/t/topic/202 "2017-08-04T01:28:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

作者： ![xxliu](https://www.shiqidu.com/user_avatar/www.shiqidu.com/xxliu/32/501_2.png) [@xxliu](https://www.shiqidu.com/u/xxliu)\
发布日期： [2017 年8 月 4 日 01:28 UTC](https://www.shiqidu.com/t/topic/202/1 "2017-08-04T01:28:21Z")

</div>

```shell
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

```

---

<div class="post-metadata">

作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)\
发布日期： [2017 年8 月 4 日 01:40 UTC](https://www.shiqidu.com/t/topic/202/2 "2017-08-04T01:40:45Z")

</div>

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

---

<div class="post-metadata">

作者： ![fengzy](https://www.shiqidu.com/user_avatar/www.shiqidu.com/fengzy/32/507_2.png) [@fengzy](https://www.shiqidu.com/u/fengzy)\
发布日期： [2017 年8 月 4 日 07:01 UTC](https://www.shiqidu.com/t/topic/202/3 "2017-08-04T07:01:46Z")

</div>

@saonian:

---

<div class="post-metadata">

作者： ![Eureka](https://www.shiqidu.com/user_avatar/www.shiqidu.com/eureka/32/5_2.png) [@Eureka](https://www.shiqidu.com/u/Eureka)\
发布日期： [2017 年8 月 4 日 07:24 UTC](https://www.shiqidu.com/t/topic/202/4 "2017-08-04T07:24:59Z")

</div>

@fengzy: 召唤我何事啊？老铁
