How can I find exactly which PHP scripts are taking up all server resources?

If the web server is running slow, is there a way to see which files are responsible?

Edited:

Debian OS with Apache2 web server


Solution 1:

Have you considered using a profiler?

See:

http://blogs.reucon.com/srt/2007/12/13/profiling_php_applications.html

http://www.sitepoint.com/blogs/2007/04/23/faster-php-apps-profile-your-code-with-xdebug/

Solution 2:

If you are trying to figure out which of your user's script is eating your resources, you could use the apache itk mpm, which runs vhosts under a user/group of your choice. Each request forks an apache process running as that user. Thus the ps/top output would show which user is running the script (and thus which vhost) - The downside is that itk is significantly slower of course. - Since with the normal prefork or worker mpm's apache handles multiple requests in one process for performance, it's harder to tell which requests are eating up your mojo.

If you're trying to find out exactly which functions in a php application are using up your resources, then you'll have to use xdebug. Obviously the xdebug profiling method would be a one time inspection for one php app, and not a solution for finding out which script belonging to which user in a shared hosting environment is consuming cpu time.