John Quilder John Quilder - 4 months ago 8
Bash Question

Capture STDIN / STDERR / STDOUT of a process AFTER it's been started, using command line?

Thanks! My usercase: I started a lengthy interactive 'configure' process (say under 'screen'), and then realised I need to always answer 'no' until I see a particular keyword. Seems a waste of time to do this by hand (not to say that I can easily miss the keyword..)

Thus it seems I want to pipe (a copy of) STDERR / STDOUT to a filter,
and also be able to inject into the STDIN of a (console) process,
AFTER it's been started, using command line? Is there a ready-made solution?

The following tools seem help. To capture output, use

strace -ewrite -p $PID


It's not that clean (shows lines like: write(#,) ), but works!
But does it say handle UTF8 correctly?

To redirect the output, do something like

printf '..input..' >/dev/pts/33


But it is not clear how to find the right device..

Answer

Solved in Linux (apparently Linux-specific):

reptyr -s PID 

attaches a process to another terminal and/or exposes its input and output as pipes.

Comments