I need to run a program and gather its output to stdout. This program (socat) needs to run in the background for the duration of the python script. Socat sits in dameon mode once it's run, but first it outputs some lines to stdout that I need for the rest of my script.
Command: socat -d -d PTY: PTY:
Output:
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/1
2011/03/23 21:12:35 socat[7476] N PTY is /dev/pts/2
2011/03/23 21:12:35 socat[7476] N starting data transfer loop with FDs [3,3] and [5,5]
...
I basically want to run that at the start of my program and leave it running till script termination, but I need to read the two /dev/pts/X names into python.
Can anyone tell me how to do this?
I came up with this which just hangs, I guess because it's blocking for the child process to terminate.
#!/usr/bin/python
from subprocess import Popen, PIPE, STDOUT
cmd = 'socat -d -d PTY: PTY: &'
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
output = p.stdout.read()
# Process the output
print(output)
Thanks for any help
EDIT: Seems it may write to stderr, but the script still just hanges with and without the & even reading from stderr.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…