1. my colleague ran a php crawl, which would lead to a very high sleep, which is about several thousand levels. mysql will go down. wrote crontab # crontab-e ** 1 *** shdetect_php.shdetech_php content #! Binbashhost_dir 'cdsh' proc _ namemysqlbug_time 'date-R' pid0proc _ num () {n
1. my colleague ran a php crawl, which would lead to a very high sleep, which is about several thousand levels. mysql will go down. crontab # crontab-e **/1 ***/sh/detect_php.sh detech_php #! /Bin/bashhost_dir = 'CD/Sh' proc _ name = mysqlbug_time = 'date-R' pid = 0proc_num () {n
1. My colleague ran a php crawl, which would lead to a very high sleep value, which is about several thousand levels. mysql would then be down.
A crontab is written.
# Crontab-e
* */1 * * * /sh/detect_php.sh
Detech_php content
#!/bin/bashhost_dir=`cd /sh`proc_name="mysql"bug_time=`date -R`pid=0proc_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_numnumber=$?if [ $number -eq 0 ]then./restart_php_mysql.shproc_idecho " Kill the php and new mysql pid is : ${pid} : ${bug_time} " >> php_mysql.log 2>&1elseproc_idecho " The mysql is working , ${bug_time} " >> php_mysql.log 2>&1echo "it's ok!"fi
The script explains that this script is very simple. It is used to determine whether the mysql process is 0. If it is 0, kill php, restart mysql, and write a log.
Restart_php_mysql.sh content
#!/bin/shkillall -9 phpservice mysql restart
Pkill will cause the script to be terminated directly after killing the process. Therefore, pkill is not used here.
Improved
#!/bin/bashhost_dir=`cd /sh`log_dir="/var/log/php_mysql_log/"proc_name="mysql"bug_time=`date -R`pid=0now_time=`date +%F`log_file="${log_dir}${now_time}.log"log_detect(){if [ ! -d "$log_dir" ];thenmkdir "$log_dir"fiif [ ! -f "$log_file" ]thentouch "$log_file"fi}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_numnumber=$?if [ $number -eq 0 ]then./restart_php.shproc_idlog_detectecho " Kill the php and new mysql pid is : ${pid} : ${bug_time} " >> ${log_dir}${now_time}.log 2>&1elseproc_idlog_detectecho " The mysql is working ,${pid} : ${bug_time} " >> ${log_dir}${now_time}.log 2>&1echo "it's ok!"fiThe updated content is to create logs by time.