40

I'm trying to run strace trough ccze, and the pipe doesn't work as expected.

The command-line I'm running to test is sudo strace -p $(pgrep apache2) | grep open, and all lines are output, ignoring grep.

Is there something special about strace that causes this behavior?

63

strace prints its traces on standard error, not on standard output. That's because it's common to want to redirect the standard output of the program, but usually not a problem that strace's stderr and the program's stderr are mixed.

So you should redirect strace's stderr to stdout to be able to pipe it:

sudo strace -p $(pgrep apache2) 2>&1 | grep open

except that what you're really looking for is

sudo strace -p $(pgrep apache2) -e open
5
  • it works!! thank you! - I wasn't actually trying to look only at open calls, I just put that as an example, what I'm really trying to do is color highlighting
    – Andrei
    Sep 15 '12 at 0:03
  • I am getting strace: Invalid process id: '-e' with the last command. I am on version 4.8. 2010-03-30. Jun 29 '17 at 19:13
  • Ahh, I actually have httpd (RHEL). Jun 29 '17 at 19:21
  • 1
    @ElijahLynn Replace apache2 by the name of the process you're interested in. Check that pgrep prints a single PID, otherwise pick one and run e.g. sudo strace -p 1234 -e open Jun 29 '17 at 20:34
  • @Andrei, what about using vim color syntax highlighting? strace $CMD 2>&1 > /dev/null | vim -c ':set syntax=strace' -.
    – Pablo A
    Aug 27 '17 at 14:49

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.