7

I'd like to know a way of inspecting HTTPD processes to find which PHP script is running on them.

I already did a "netstat" and found that some processes held DB and Network sockets for too long and now i want to know what scripts are causing it.

Btw, i'm using Linux.

LatinSuD
  • 901
  • 1
  • 8
  • 17

2 Answers2

15

You need to have Apache module mod_status enabled (CentOs main Apache config file is located at /etc/httpd/conf/httpd.conf)

LoadModule status_module modules/mod_status.so

with option ExtendedStatus on (this is to be set in the same config file as above)

# ExtendedStatus controls whether Apache will generate "full" status
# information (ExtendedStatus On) or just basic information (ExtendedStatus
# Off) when the "server-status" handler is called. The default is Off.
#
ExtendedStatus On

and some access rights set for that (replace below XXX.XXX.XXX.XXX with your IP - this is to be found in the same config file as above)

# Allow server status reports generated by mod_status,
# with the URL of http://servername/server-status
# Change the ".example.com" to match your domain to enable.
#
<Location /server-status>
    SetHandler server-status
    Order deny,allow
    Deny from all
    Allow from localhost 127.0.0.1 XXX.XXX.XXX.XXX
</Location>

Finally you will be seing what each HTTPD process is doing by accessing http://your-server-name/server-status

This will show the pids and URLs currently being processed in the way presented here.

Bogdan
  • 103
  • 3
symcbean
  • 21,009
  • 1
  • 31
  • 52
  • Someone upvoted this today, so I came back to have a look at what I wrote. Reviewing the question and my answer...yes I answered the question but solving the problem would be better served by logging %D and analyzing the data. – symcbean Jun 27 '16 at 23:20
6

The suggested mod_status is a great alternative to see what process is doing what at the moment, you enable it (and usually add Allow statements to permit you to view it from the IP range you access the webserver from) and then browse to http://site.name/server-status and get nice output. An example can be seen at Apaches own site: http://www.apache.org/server-status

Another useful tool to see whats going on with a process is lsof, if you have a known PID that "hangs" you can type lsof -p <pid> to see whats going on with that one. To match all processes you can type something like lsof -c apache or lsof -c httpd. Its a very versatile tool with lots of options for what you want to see.

Finally you have strace that can attach to running processes to see what they are doing at the moment with system calls, etc. strace -p <pid> for example. Warning, this CAN sometimes hang the running process, so keep an eye open and restart if needed.

lsof(8) man page: http://linux.die.net/man/8/lsof
strace(1) man page: http://linux.die.net/man/1/strace

Mattias Ahnberg
  • 4,139
  • 19
  • 19
  • Does lsof show the source file name when using a PHP cache? – symcbean Jan 17 '12 at 15:58
  • Depends on the implementation and how files are open/held, strace would be more detailed, if you call it with -f (fork) it will follow child processes too which might catch a webserver -> childprocess -> cache -> etc. call. But hard to say exactly without you playing with it and trying it out to learn the tools features! – Mattias Ahnberg Jan 17 '12 at 17:43
  • I found this article when searching under the context of running on Windows... Of course, `lsof` is not available on Windows, so in case anyone gets here the same way: http://stackoverflow.com/questions/15708/how-can-i-determine-whether-a-specific-file-is-open-in-windows – simonhamp Jan 30 '14 at 16:11
  • @symcbean I couldn't see it on my ubuntu server, but the first file was the current-directory which at least told me that it was our beta site and thus safe to kill a rogue process. Also it showed the connection to the client (browser) and the IP / socket details. – scipilot Jan 21 '15 at 04:28