1

On a Linux machine, if I have the pid of a process, I can write to the stdin of that process using something like:

/proc/<pid>/fd/0

but on MacOS, this doesn't appear possible. I am told that I should use mkfifo / named pipes.

However using named pipes is making the problem harder to solve. Is there a way on MacOS to store a reference to a fd on the filesystem if you control the start of the process?

For example, if I do:

#!/usr/bin/env node
node script.js &
NODEJS_PID=$!                

I can easily get the pid of this process, represented above by the NODEJS_PID var. However, is there a way to capture a reference to the file descriptor of the stdin to this nodejs process?

If I can get the path to the fd that would be cool, because I need to write to that fd from another process altogether.

1 Answer 1

2

Since you know the PID on OS X, lsof might work for you:

lsof -p 12345 | awk '$4=="0u"{print $NF}'
/dev/ttys039
# then you can write to the FD
echo "hi" > /dev/ttys039
6
  • thanks can you comment on the lsof line and let people know what it does? Commented Jul 7, 2017 at 23:06
  • 1
    lsof lists the open files on a system. The -p option lists open files for a particular process id. Commented Jul 7, 2017 at 23:09
  • just curious, how do you know this is the fd for stdin? Commented Jul 7, 2017 at 23:16
  • 1
    @AlexanderMills But note that the example shown doesn't actually do what you want. If you write to a terminal, that displays the text on the terminal, it doesn't feed input to a program that's reading from the terminal. This goes whether you refer to the terminal directly through its /dev entry or via a Linux magic link under /proc/$pid/fd. It works with a pipe because pipes aren't bidirectional. Commented Jul 7, 2017 at 23:46
  • I think I follow what you are saying Gilles, so mkfifo is really the best way to do this? Commented Jul 8, 2017 at 0:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.