0

I want to write to the stdin of a running process (not Java). How can I get the Process object or the OutputStream directly? Runtime.getRuntime() only helps me spawn new things, not find existing processes.

dimo414
  • 47,227
  • 18
  • 148
  • 244

1 Answers1

2

This looks possible on Linux, no idea about elsewhere. Searching for "get stdin of running process" revealed several promising looking discussions:

Essentially, you can write to the 0th file descriptor of a process via /proc/$pid/fd/0. From there, you just have to open an OutputStream to that path.

I just tested this (not the Java part, that's presumably straightforward) and it worked as advertized:

Shell-1 $ cat

This blocks, waiting on stdin

Shell-2 $ ps aux | grep 'cat$' | awk '{ print $2 }'
1234
Shell-2 $ echo "Hello World" > /proc/1234/fd/0

Now back in Shell-1:

Shell-1 $ cat
Hello World

Note this does not close the process's stdin. You can keep writing to the file descriptor.

Community
  • 1
  • 1
dimo414
  • 47,227
  • 18
  • 148
  • 244