Redirect strace to file

54,565

Solution 1

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.

Solution 2

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

Solution 3

 ps auxw | grep sbin/apache | awk '{print"-p " $2}' | xargs strace 2>> trace.txt
Share:
54,565
ofitz
Author by

ofitz

Updated on September 18, 2022

Comments

  • ofitz
    ofitz over 1 year

    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
    
  • ofitz
    ofitz about 11 years
    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.