23

Am trying to trace apache2. These are the commands i'm trying to run.

ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace >> trace.txt

I tried

(ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace ) >> trace.txt

or

ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace | xargs >> trace.txt

4 Answers 4

25

Any particular reason why you can't use the -o flag?

ps auxw | grep sbin/apache | awk '{print " -p " $2}' | xargs strace -o /tmp/trace.txt

Also, you should use some regex trick to not catch your grep process in the process list.

ps auxw | grep 'sbin/[a]pache' ....

Also remember that in the specific case of apache,

  1. Apache is (usually) multiprocess, do you want to get the parent or one of the children? I'd add the '-f' flag to strace, which (among other things) inserts the PID in the output.
  2. You can get the process ID of the main parent process from the PID file.

    e.g. strace -o /tmp//trace.txt -f $(< /path/to/apache.pid)

So, that's the question you're asking.. but what are you trying to do? Debug on startup? strace a child process serving a request? Also look at the -X flag to apache.

1
  • Hi Rich thanks for your answer. What i'm trying to do is see if there's a hiccup in the CPU usage. However '-c' option is not that useful for some reason. I also checked the trace.txt for timestamp for any weird increment and i don't see any.
    – Marin
    Mar 6, 2013 at 18:00
11

In general, strace "can be redirected to a file" but it writes its output to stderr (mixing it with the stderr of the program), so it'd be like

strace progname 2>my_strace_output

If you want "all output" to go to a file:

strace progname 1>my_strace_output 2>&1

if you want to be able to "grep" it:

strace progname 2>&1 | grep ...

however be aware if you grep and send it to a file lots of buffering happens: https://unix.stackexchange.com/questions/338330/grepping-strace-output-gets-hard

1
 ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace 2>> trace.txt
0

You can also direct standard out to a file this way:

(time /bin/ls) &> /tmp/time.txt

or for debugging a php script:

(strace php /path/to/www/script.php) &> strace_output.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .