Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a process's stdin by a process id? [closed]

Tags:

python

linux

I know i can get process's stdin use subprocess in python like:

import subprocess
f = subprocess.Popen('python example.py',stdin=subprocess.PIPE)
f.stdin.write('some thing')

but I want only know the pid which i want to write to the process's stdin how can i do this?

like image 351
timger Avatar asked Sep 07 '12 06:09

timger


People also ask

Can stdin be closed?

you can just fclose(stdin), it will call close() on the file handle.

When stdin is closed?

stdin is closed when the process decides to close it period. Now, when the stdin of a process is the reading end of a pipe, the other end of the pipe can be open by one or more other processes.

Can a process write to its own stdin?

You can't "push to own stdin", but you can redirect a file to your own stdin.

How do you submit an input to a running process?

It is possible to send input text to a running process without running the screen utility, or any other fancy utility. And it can be done by sending this input text to the process' standard input "file" /proc/PID#/fd/0 . However, the input text needs to be sent in a special way to be read by the process.


1 Answers

Simply write to /proc/PID/fd/1:

import os.path
pid = os.getpid() # Replace your PID here - writing to your own process is boring
with open(os.path.join('/proc', str(pid), 'fd', '1'), 'a') as stdin:
  stdin.write('Hello there\n')
like image 50
phihag Avatar answered Oct 05 '22 00:10

phihag