Redirect strace to file
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,
- 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.
-
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