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?
you can just fclose(stdin), it will call close() on the file handle.
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.
You can't "push to own stdin", but you can redirect a file to your own stdin.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With