commands.getoutput hangs

Brian bbird at hotpop.com
Fri Nov 8 06:47:57 EST 2002


I'm trying to get some output from a shell script running within
python. Normally I can do this really easily with commands.getoutput.
However, in this case, the shell script spawns another process which
continues to run in the background even when the it exits. eg.

test.sh:
 echo "Starting"
 /usr/bin/my_server &
 echo "Server started"

python:
>>> import commands
>>> commands.getoutput("test.sh")

I want to get the output of test.sh ("Starting" and "Server started")
but I don't care about the output of my_server (which runs forever).
If I use os.system("test.sh") it exits immediately with the output
shown on screen. But commands.getoutput hangs, presumably because the
pipe is trying to read from the child process created by test.sh. Is
there any way to read from just the test.sh process and not the
my_server process?

I've tried os.popen, os.popen2, commands.getoutput etc. on python1.5
and python2.1.1

Any suggestions?

Thanks,

Brian



More information about the Python-list mailing list