46

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?

Andrei
  • 1,713
  • 1
  • 15
  • 18

1 Answers1

70

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
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • 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 00:03
  • 1
    I am getting `strace: Invalid process id: '-e'` with the last command. I am on version 4.8. 2010-03-30. – Elijah Lynn Jun 29 '17 at 19:13
  • Ahh, I actually have httpd (RHEL). – Elijah Lynn 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` – Gilles 'SO- stop being evil' Jun 29 '17 at 20:34
  • 1
    @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