By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

Getting both PID and output from a command

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message news:<3c**************************@posting.google. com>...
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)


Take a look in PyPi for popen5. It may do what you want, but only in
Linux: the Windows version isn't complete yet.

S
Jul 18 '05 #2

P: n/a
----- Original Message -----
From: "Stewart Midwinter" <st*****@midwinter.ca>
Subject: Re: Getting both PID and output from a command

hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message

news:<3c**************************@posting.google. com>...
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)


Take a look in PyPi for popen5. It may do what you want, but only in
Linux: the Windows version isn't complete yet.


I'll take a look - thanks....
Hugh Macdonald
Jul 18 '05 #3

P: n/a
In article <3c**************************@posting.google.com >,
hu**@brokenpipefilms.com (Hugh Macdonald) wrote:
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)


You're close there. You're creating a class instance
that has the pid, but os.popen3 hides it from you.
See popen3.py in the library for details.

Donn Cave, do**@u.washington.edu
Jul 18 '05 #4

P: n/a
hu**@brokenpipefilms.com (Hugh Macdonald) wrote in message news:<3c**************************@posting.google. com>...
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


Hi,

The Pexpect library lets you run external commands.
You get output from a command and you can get the pid.
http://pexpect.sourceforge.net/
You should be able to write code like this:

import pexpect
child = pexpect.spawn (command.split()[0], command.split())
print child.pid
try:
print child.read()
except pexpect.TIMEOUT:
child.kill (9)

One problem is that the stdout and stderr are merged into a single stream.
This is a limitation of the Python pty library. Oops...
You will still be able to read the error, but you can't read it
separately from stdout.

Note, I wouldn't trust using a pipe. You will not see any data on the pipe
until the child decides to flush the pipe. There is no way to force the
child to flush it's stdout (your stdin from your point of view).
Pipes are bad for working with child apps that use the stdio libary.
You need a pty for that.

Yours,
Noah
Jul 18 '05 #5

P: n/a
I suspect I'll probably use Donn Cave's suggestion when I give it a go on
monday - I'd rather not use any external modules if I can help it, and I
know that stderr gives me output in a format that I can read (I know exactly
which command I want to run here, and I have the program working properly
except for not being able to stop if before it finishes on its own.....)

Hugh Macdonald

----- Original Message -----
From: "Noah" <no**@noah.org>
Subject: Re: Getting both PID and output from a command

The Pexpect library lets you run external commands.
You get output from a command and you can get the pid.
http://pexpect.sourceforge.net/
You should be able to write code like this:

import pexpect
child = pexpect.spawn (command.split()[0], command.split())
print child.pid
try:
print child.read()
except pexpect.TIMEOUT:
child.kill (9)

One problem is that the stdout and stderr are merged into a single stream.
This is a limitation of the Python pty library. Oops...
You will still be able to read the error, but you can't read it
separately from stdout.

Note, I wouldn't trust using a pipe. You will not see any data on the pipe
until the child decides to flush the pipe. There is no way to force the
child to flush it's stdout (your stdin from your point of view).
Pipes are bad for working with child apps that use the stdio libary.
You need a pty for that.

Yours,
Noah
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #6

P: n/a
Hugh Macdonald <hu**@brokenpipefilms.com> wrote:
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)


To get stdout and stderr as separate pipes and also have the child's
PID, you want to use the Popen3 class from the popen2 module. See:

http://www.python.org/doc/current/li...le-popen2.html
http://www.python.org/doc/current/li...3-objects.html

for details.

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #7

P: n/a
In article <ma************************************@python.org >,
Hugh Macdonald <Hu***********@brokenpipefilms.com> wrote:
I suspect I'll probably use Donn Cave's suggestion when I give it a go on
monday - I'd rather not use any external modules if I can help it, and I
know that stderr gives me output in a format that I can read (I know exactly
which command I want to run here, and I have the program working properly
except for not being able to stop if before it finishes on its own.....)

Jul 18 '05 #8

P: n/a
Fair enough - thanks for the suggestion....

I have actually already gone the other way, which works perfectly, thanks.
If I find problems with the method I'm using, then I will certainly take a
look at this one...

Hugh Macdonald

----- Original Message -----
From: "Cameron Laird" <cl****@lairds.com>
To: <Hu***********@brokenpipefilms.com>
Sent: Tuesday, March 30, 2004 2:14 AM
Subject: Re: Getting both PID and output from a command

In article <ma************************************@python.org >,
Hugh Macdonald <Hu***********@brokenpipefilms.com> wrote:
I suspect I'll probably use Donn Cave's suggestion when I give it a go on
monday - I'd rather not use any external modules if I can help it, and I
know that stderr gives me output in a format that I can read (I know exactlywhich command I want to run here, and I have the program working properly
except for not being able to stop if before it finishes on its own.....)

.
.
.
Please understand that, as external modules go, Pexpect
is easy to like, because it's "pure Python". It's just
some Python source code. You can choose to put it all
inside *your* program, then there's nothing "external".
If you wish, just think of it as a particularly long
Usenet reply that has already been tested.
--

Cameron Laird <cl****@phaseit.net>
Business: http://www.Phaseit.net

Jul 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.