Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to troubleshoot the stuck execution of PHP script

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to troubleshoot the problem of PHP script execution stuck. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Find a problem

Recently, we suddenly found from the monitoring that the load of a machine we serve is higher than that of other machines in the same room, while there is no difference in inflow and outflow traffic. Further inspection found that there is a machine in each computer room with the same phenomenon. After combing, we found that these machines with problems ran some more PHP scripts than normal machines, so we guessed that there was something wrong with the execution of the script.

Solve the problem

After logging in to the machine, execute the top command, and sure enough, you find that there is a CPU-intensive PHP process, and then execute the following command to find that there is a PHP script started by crontab that has been executed for a long time:

Ps aux | grep 'php' | grep-v' php-fpm'

Due to the similar situation in which the execution of PHP scripts got stuck before, it was suspected that the Mysql query across the computer room caused the Mysql connection to get stuck when the network jitter, so of course, all the stuck processes were kill off, and from the load point of view, the machine immediately returned to normal, so contentedly went to do something else.

After a period of time, after brushing the monitoring, I found that the problem appeared again. After commenting out the crontab and kill the process, the manual execution of the problem script could stably reproduce the problem! It seems that the problem is too simple. Try to use the strace command to see what the stuck process is doing right now:

[tabalt@localhost] sudo strace-p 13793Process 13793 attached-interrupt to quit

There is no output! Then use netstat to see if any ports are open for this process:

[tabalt@localhost ~] sudo netstat-tunpa | grep 13793tcp 00 192.168.1.100 grep 13793tcp 38019 192.168.1.101 ESTABLISHED 13793/phptcp 00 192.168.1.100 tunpa 47107 192.168.1.102

You can see that the process has opened two ports, established a connection with Mysql and Redis, and is in the state of connection establishment (ESTABLISHED) and active closing of the connection (CLOSE_WAIT). At first glance, it does seem that the connection with the database is stuck, but because we have been fooled, we use tcpdump to grab the package to see the interaction between the process and the database:

Tcpdump-I eth0 host 192.168.1.101 and port 3306-w ~ / mysql.cap

After grabbing it for a long time, there is no output in the ~ / mysql.cap file. Is there no interaction between the process and Mysql? So why is the connection establishment not closed? It seems that we can only trace the execution of the script from scratch:

First of all, in order to have time to strace to the process, output the process's pid and sleep 10s at the beginning of the PHP script:

Echo getmypid (); sleep (10)

Then start tcpdump to prepare for the interaction between the native and Mysql.

Finally, execute the PHP script, copy the output pid, and execute the strace command in a new window.

Now both strace and tcpdump have content! From the strace results, there is no more poll after recvfrom, but there is nothing wrong with it:

/ /... poll ([{fd=4, events=POLLIN | POLLERR | POLLHUP}], 1, 1471228928) = 1 ([{fd=4, revents=POLLIN}]) recvfrom (4, ": / / xxx.com/\ 0\ 0\ 23jiadia", 271, MSG_DONTWAIT, NULL, NULL) = 271poll ([{fd=4, events=POLLIN | POLLERR | POLLHUP}], 1, 1471228928) = 1 ([{fd=4, revents=POLLIN}]) recvfrom (4, "_ b?ie=UTF8&node=658390051\ 0\ 0008www.", 271, MSG_DONTWAIT, NULL, NULL) =

Judging from the packet capture result, after executing two SQL query statements, the process did not send the query request package again. From the log of the SQL statement recorded by the program, we also found that only two were executed:

Select * from sites where type = 1 limit 50 * from sites where type = 2 limit 50

But from these phenomena, still can not see any clue, have to sacrifice the ultimate law: output debugging! Take a look at the code and add output statements in key places, so the code looks like this:

Echo ("start foreach\ n"); foreach ($types as $type) {echo ("foreach $type\ n"); $result [$type] = $this- > getSites ($type);} echo ("end foreach\ n")

The output after execution is as follows. When querying the URL with type 2, it gets stuck:

Start foreachforeach 1foreach 2

Begin to suspect that there is something wrong with the called getSites () method, as follows:

$sites = array (); / / omit the code to query from the database $siteNum = 8; / / omit the code read from the configuration $urlKeys = $result = array (); for ($I = 0; $I < $siteNum; $iTunes +) {do {$site = array_shift ($sites); $urlKey = md5 ($site ['url']);} while (array_key_exists ($urlKey, $urlKeys)); $urlKeys [$urlKey] = 1; $result [] = $site;} $result

It turns out that in order to achieve the goal of writing 2 loops with 8 non-repetitive URLs, if only 7 URLs are not repeated in the result, there will be one empty, and less than 7 will have an endless loop! So check the number of URLs whose type is 2, and sure enough there are only 6!

This is the end of this article on "how to troubleshoot the problem of PHP script execution stuck". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report