1.同事跑了一個php的抓取, 這個會導致非常高的sleep值,大概到好幾千的水平.進而mysql會down掉.
寫了個crontab
#crontab -e
[php]
* */1 * * * /sh/detect_php.sh
* */1 * * * /sh/detect_php.sh
detech_php 的內容
[php]
#!/bin/bash
host_dir=`cd /sh`
proc_name="mysql"
bug_time=`date -R`
pid=0
proc_num()
{
num=`ps -ef | grep $proc_name | grep -v grep | wc -l`
return $num
}
proc_id()
{
pid=`ps -ef | grep $proc_name | grep -v grep | awk '{print $2}'`
}
proc_num
number=$?
if [ $number -eq 0 ]
then
./restart_php_mysql.sh
proc_id
echo " Kill the php and new mysql pid is : ${pid} : ${bug_time} " >> php_mysql.log 2>&1
else
proc_id
echo " The mysql is working , ${bug_time} " >> php_mysql.log 2>&1
echo "it's ok!"
fi
#!/bin/bash
host_dir=`cd /sh`
proc_name="mysql"
bug_time=`date -R`
pid=0
proc_num()
{
num=`ps -ef | grep $proc_name | grep -v grep | wc -l`
return $num
}
proc_id()
{
pid=`ps -ef | grep $proc_name | grep -v grep | awk '{print $2}'`
}
proc_num
number=$?
if [ $number -eq 0 ]
then
./restart_php_mysql.sh
proc_id
echo " Kill the php and new mysql pid is : ${pid} : ${bug_time} " >> php_mysql.log 2>&1
else
proc_id
echo " The mysql is working , ${bug_time} " >> php_mysql.log 2>&1
echo "it's ok!"
fi
腳本解釋,這個腳本非常簡單的. 就是判斷mysql進程是否為0, 如果為0,那麼就去殺掉php,然後重啟mysql.並且寫入一個日志.
restart_php_mysql.sh 的內容
[php]
#!/bin/sh
killall -9 php
service mysql restart
#!/bin/sh
killall -9 php
service mysql restart
pkill會導致腳本在殺完進程後直接terminated. 所以這裡不使用pkill.