Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 267
John Quilder Avatar asked Jan 28 '12 18:01

John Quilder


People also ask

How do I redirect the output of an already running process?

The way we can redirect the output is by closing the current file descriptor and then reopening it, pointing to the new output. We'll do this using the open and dup2 functions. There are two default outputs in Unix systems, stdout and stderr. stdout is associated with file descriptor 1 and stderr to 2.

What is stdin stdout and stderr in Linux?

The Linux Standard Streams In Linux, stdin is the standard input stream. This accepts text as its input. Text output from the command to the shell is delivered via the stdout (standard out) stream. Error messages from the command are sent through the stderr (standard error) stream.

What is stdin stdout stderr?

stdin − It stands for standard input, and is used for taking text as an input. stdout − It stands for standard output, and is used to text output of any command you type in the terminal, and then that output is stored in the stdout stream. stderr − It stands for standard error.

How would you redirect a command stderr to stdout?

Understanding the concept of redirections and file descriptors is very important when working on the command line. To redirect stderr and stdout , use the 2>&1 or &> constructs.


1 Answers

Solved in Linux (apparently Linux-specific):

reptyr -s PID 

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

like image 70
John Quilder Avatar answered Sep 21 '22 13:09

John Quilder