9

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?

0x90
  • 39,472
  • 36
  • 165
  • 245
timger
  • 944
  • 2
  • 13
  • 31

1 Answers1

12

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')
phihag
  • 278,196
  • 72
  • 453
  • 469