0

Let's say that I start a cat process to wait for some input. e.g.,

$ cat > out.log

In another terminal, I can identify its PID and feed data into its File Descriptor 0 (zero = STDIN), like this:

$  echo "hello" > /proc/2357/fd/0

And then the first terminal reacts:

$ cat > out.log
hello

A couple of questions: 1)The "hello" string shows up in the actual terminal instead of being redirected to the file, is that due to the nature of the cat program (it just focuses on the tty/foreground)?

2) I can't tail the STDOUT of that same process:

tail -f /proc/2357/fd/1
...

Nothing shows up. Any conjectures on what is going on here?

2 Answers2

4

/proc/2357/fd/0 is file descriptor 0 of the cat process, which is the terminal itself. /proc/2357/fd/1 is file descriptor 1 of the cat process, which you connected to the file out.log.

Now if you echo something to /proc/2357/fd/0, it will go to the terminal, because that's the file the descriptor refers to.

If you echo something to /proc/2357/fd/1, it will go into out.log, but not via cat!

You cannot echo something to the process in this way.

ctrl-d
  • 952
2

You might want to take a look at the tee(1) command, which is basically a T-junction for unix commands: it redirects its stdin to both stdout and a file (which you can then tail -F)

znpy
  • 197
  • It does sound like an interesting alternative just to play with file descriptors and make all this knowledge more tangible. However.. I wasn't able to achieve what I was looking for with the tee command either. I've also tried cat | tee output.txt and checked the stdout = /proc/<PID>/fd/1 of the tee process, but there's no reaction in my tail -f. Thoughts? – theMarceloR Jul 01 '18 at 15:16