Getting both PID and output from a command

Hugh Macdonald hugh at brokenpipefilms.com
Fri Mar 19 05:59:58 EST 2004


I'm calling a command from within a python script and I need to be
able to both catch the output (stdout and stderr) from it and also
have the PID (so that I can kill it)

I can do one or other of these, but I can't find any way to do them
both at the same time.

So far, I've got the following options:


To get the output from the command:

>>> fdin, fdout, fderr = os.popen3(command)


Or to get the PID so that I can kill it:

>>> pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())


I've tried doing the following to grab stderr (I only need stderr, not
stdout in this case)

>>> fderr = os.pipe()
>>> sys.stderr = fderr
>>> pid = os.spawnvp(os.P_NOWAIT, command.split()[0], command.split())

But this doesn't seem to help.... It still just outputs stderr to the
terminal and when I try to do fderr.read(1) I don't get anything...

Any help would be very welcome


Hugh Macdonald



More information about the Python-list mailing list