473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Getting both PID and output from a command

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
8 3664
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
----- 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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Sami Viitanen | last post by:
Hello, I'm using os.popen and read for reading command input to string but the string doesn't contain the same output that running the command manually or with os.system contains. with...
16
by: Chris Maloof | last post by:
Hello, Does anyone know how I can read the ASCII text from a console window (from another application) in WinXP? It doesn't sound like a major operation, but although I can find the window via...
2
by: gregpinero | last post by:
Hey everyone, I'm trying to call a system command "svnlook log \arms" from within python and process the results. However I'm having trouble getting the results of the command back into python....
6
by: Christian Convey | last post by:
Hello, I've got a program that (ideally) perpetually monitors sys.stdin for lines of text. As soon as a line comes in, my program takes some action. The problem is, it seems like a very large...
0
by: ruju00 | last post by:
I am getting an error in Login() method of the following class FtpConnection public class FtpConnection { public class FtpException : Exception { public FtpException(string message) :...
22
by: SF | last post by:
Hello All, In a windows C learning project I am wokring on I use the system function to run a command, I want to suck the results into a vairable. The system function seems to only return an...
5
by: noLoveLusT | last post by:
hi everyone i am very very new to the sql server (2 days actually and ) so far i learned creating SPs etc but couldnt workout how to get return value from my prodecure my sp as follows...
1
by: leescriven | last post by:
Hi, Thi is my first post on this forum, and I'm hoping that there is a guru out there who can help me with an annoying problem I am having. I have written a Stored Procedure that relies heavily...
1
by: Mike Lester | last post by:
I have a need for a stored procedure to return a recordset AND an output parameter that contains the count of records in the recordset. I can get either but not both. (ie. if there is a select...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.