0

This question already has an answer here:

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?

marked as duplicate by muru, Jesse_b, Romeo Ninov, user34720, G-Man Jul 22 '18 at 4:53

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

migrated from serverfault.com Jul 19 '18 at 6:22

This question came from our site for system and network administrators.

3

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

1

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)

  • 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 1 '18 at 15:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.