One simple question: I have Linux process ID, which is 3789. How can I send 'ENTER' to this process by using Python?
Asked
Active
Viewed 1,795 times
1
-
Is the process waiting on reading stdin? Is it connected to a terminal, a GUI? Was it started by the Python program? – cdarke Sep 10 '15 at 07:45
-
Yes, the process is waiting for 'ENTER'. It was started by Python script, and it is connected to terminal. – user5311867 Sep 10 '15 at 07:48
-
You need to run the program using a pipe, preferably using `subprocess.Popen`. Just send it a `'\n'`. Or are you asking for a process that is currently running? – cdarke Sep 10 '15 at 07:50
-
also check out `pexpect`. – gauteh Sep 10 '15 at 07:50
1 Answers
0
You are able to do such thing but with proc name using Python subprocess but not with 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

wolendranh
- 4,202
- 1
- 28
- 37
-
3
-
1subprocess stdin doesn't have print(). Also, by default, the program to execute is the first item in args if args is a sequence, you can't use process name but executable path – Francesco Jul 21 '21 at 10:04