How to find out what process is writing to STDOUT

debuggingopen filesprocessstdouttrace

I have two instances of a process running. One of them is "frEAkIng oUT!" and printing errors non stop to STDOUT.

I want to kill the broken process but I have to make sure I don't terminate the wrong one. They were both started about at the same time and using top I can see they both use about the same amount of memory and CPU. I can't seem to find anything that points to which process is behaving badly.

The safest thing would be to figure out which process/pid is writing to STDOUT.

Is there any way to do that?

Best Answer

On Linux, assuming you want to know what is writing to the same resource as your shell's stdout is connected to, you could do:

strace -fe write $(lsof -t "/proc/$$/fd/1" | sed 's/^/-p/')

That would report the write() system calls (on any file descriptor) of every process that have at least one file descriptor open on the same file as fd 1 of your shell.