一尘不染

如何通过进程ID获取进程的标准输入?[

linux

我知道我可以像这样获得流程的stdin使用子流程python

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

但是我只想知道要写入进程的pid,我该stdin 怎么做?


阅读 307

收藏
2020-06-07

共1个答案

一尘不染

只需写信给/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')
2020-06-07