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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值