Home  >  Article  >  Backend Development  >  PHP forks too many processes, resulting in overall performance degradation. The solution to mysql down._PHP Tutorial

PHP forks too many processes, resulting in overall performance degradation. The solution to mysql down._PHP Tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:101007browse

1. A colleague ran a php crawl, which will lead to a very high sleep value, probably to the level of several thousand. Then mysql will go down.

Write a crontab

#crontab -e


[php]
* */1 * * * /sh/detect_php.sh

* */1 * * * /sh/detect_php.sh

detech_php content

[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

Script explanation, this script is very simple. It is to determine whether the mysql process is 0. If it is 0, then kill php, then restart mysql. And write a log.


Contents of restart_php_mysql.sh

[php]
#!/bin/sh
killall -9 php
service mysql restart

#!/bin/sh
killall -9 php
service mysql restart

pkill will cause the script to be terminated directly after killing the process. So pkill is not used here.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477528.htmlTechArticle1. A colleague ran a php crawl, which will lead to a very high sleep value, probably several times. Level of thousands. Then mysql will go down. I wrote a crontab #crontab -e [php] * */1 * * * /sh/detect_p...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn