473,734 Members | 2,460 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(comma nd)

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 3698
hu**@brokenpipe films.com (Hugh Macdonald) wrote in message news:<3c******* *************** ****@posting.go ogle.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*****@midwin ter.ca>
Subject: Re: Getting both PID and output from a command

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

news:<3c******* *************** ****@posting.go ogle.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**@brokenpipe films.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(comma nd)


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.washingt on.edu
Jul 18 '05 #4
hu**@brokenpipe films.com (Hugh Macdonald) wrote in message news:<3c******* *************** ****@posting.go ogle.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(comma nd)

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.or g>
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**@brokenpip efilms.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(comma nd)


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************ *************** *********@pytho n.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************ *************** *********@pytho n.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
2506
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 os.system: cvs server: Diffing //Project1 File //Project1/testiware.txt is new; current revision 3.13 File //Project1/what_is_version_control.txt is new; current revision 3.3
16
2675
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 pywin32, I haven't been able to do anything with it. I'd really just like to get the window text into a string. By "console window", I mean the sort of thing that comes up when you run "command" (although this particular one is for the game...
2
2301
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. I'm using windows if that matters. Here's what I have so far: os.system("svnlook") #works but doesn't give me the output from
6
2783
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 amount of data must accumulate on sys.stdin before even my first invocation of readline() returns. This delay prevents my program from being responsive in the way it must be.
0
3714
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) : base(message){} public FtpException(string message, Exception innerException) : base(message,innerException){}
22
2836
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 int. I would like to know how could this is done? I can see only one way currently and I know that there must be a better method. Right now I would be tempted to pipe the output of the command to a temp file and then get the data from the file....
5
2188
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 ________________________ CREATE PROCEDURE . @PageIndex INT, @PageSize INT, @Total INT OUTPUT
1
7430
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 on dynamic SQL to export a series of table/views to CSV format, then zip the files up, then it creates an series of ftp commands to put the newly created zip file onto the relevent ftp server. The list of files and destination server are held in a...
1
6000
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 then there is no output parameter) The stored procedure is: ALTER PROCEDURE test ( @Msgs nvarchar(150) OUTPUT )
0
8951
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9458
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6040
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2184
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.