2015-09-10 45 views
-1

一個簡單的問題:我有Linux進程ID,它是3789.如何通過使用Python將「ENTER」發送到此進程?如何通過PID在Python中將按鍵發送到Linux進程?

+0

進程是否在等待閱讀stdin?它連接到一個終端,一個GUI?它是由Python程序啓動的嗎? – cdarke

+0

是的,這個過程正在等待'ENTER'。它由Python腳本啓動,並且連接到終端。 – user5311867

+0

您需要使用管道運行程序,最好使用'subprocess.Popen'。只是發送一個''\ n''。還是你要求一個當前正在運行的進程? – cdarke

回答

2

您可以使用Python subprocess來處理此類事情,但使用proc名稱,但不能使用PID。

from subprocess import Popen, PIPE 
p = Popen(['proc_name', filePath], stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) 
p.stdin.print('\n') # sends Enter into process 
+0

@cdarke編輯。空格按鈕只是作爲例子 – wolendranh

+1

filePath會受到哪個文件?! – user5311867

相關問題