Find what PHP script is running on each HTTPD process

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.


Solution 1:

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.

Solution 2:

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