My apache2 is using running slowly. A single PHP file is takes long time to process. The server is live. I dont want to restart it. So I thought I attach strace to it and check whats happening.

So I run

strace -p `cat /var/run/apache2.pid `

After that I load some testing urls. Load then many times. But I didn't see any output at the strace console!

Then I remember there are many child processes. So I checked them

$ ps ax | grep apache[2] -i
 1877 ?        Ss     0:02 /usr/sbin/apache2 -k start
 2006 ?        S      0:00 /usr/sbin/apache2 -k start
 2007 ?        S      0:00 /usr/sbin/apache2 -k start
 2008 ?        S      0:00 /usr/sbin/apache2 -k start
 2009 ?        S      0:00 /usr/sbin/apache2 -k start
 2010 ?        S      0:00 /usr/sbin/apache2 -k start
 9985 ?        S      0:00 /usr/sbin/apache2 -k start
 9988 ?        S      0:00 /usr/sbin/apache2 -k start
 9997 ?        S      0:00 /usr/sbin/apache2 -k start
 9998 ?        S      0:00 /usr/sbin/apache2 -k start
 9999 ?        S      0:00 /usr/sbin/apache2 -k start

Now my question is how do I strace all of these child processes? How do I know which child process is handling the request right now?

  • Did anyone even see this question? – Shiplu Mokaddim Feb 21 '12 at 22:02

The short answer: you cannot predict which apache process will handle a request.

The simplest way to solve this problem is to dedicate an xterm to every apache process and run strace -p <pid> in it for each apache pid.

When I'm in this situation, I usually do this in my httpd.conf (on a development box):

<IfModule prefork.c>
StartServers       1  <--------
MinSpareServers    1  <--------
#StartServers       8
#MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000

This reduces the number of processes I need to monitor... obviously it's not a good solution for a production environment.

FYI, if you need to strace a process that will fork after you start stracing (for instance, a wsgi process) you can use strace -fp <pid_of_future_forker> and it will strace the forked children automagically.

  • can we just attach strace too all apache process? – slier Jan 21 '15 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.