473,385 Members | 1,958 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,385 software developers and data experts.

Grabbing the output of a long-winded shell call (in GNU/Linux)

Hello,

Suppose I want to run from within a Python GUI app some long-output
shell call. For example, from within Python I might want to call

g++ foo.cpp

I already know there are many ways to do this, e.g.,
commands.getstatusoutput('g++ foo.cpp') to name one.

The problem is that this might generate a ton of output (e.g.,
because of compilation errors), and might take a while to do so. In my
GUI, I'd like to print out the output as it's being generated, not wait
until all is done (as commands.getstatusoutput will do) and dump it at
once.

So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.
Many Thanks,

E
May 1 '07 #1
4 1309
On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.
There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Raghu.


May 1 '07 #2
dr*******@gmail.com wrote:
On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
>So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Raghu.

Many Thanks! I'll try this
May 1 '07 #3
Efrat Regev schrieb:
dr*******@gmail.com wrote:
>On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:
>>So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.
Or instead use the python subprocess module and read the commands
stdin/out/err from the Popen-object.

Diez
May 1 '07 #4
Diez B. Roggisch wrote:
Efrat Regev schrieb:
>dr*******@gmail.com wrote:
>>On May 1, 2:23 pm, Efrat Regev <efrat_re...@yahoo.comwrote:

So my question is if there's a way to "grab" the output as it's being
generated. It doesn't matter if the solution is blocking (as opposed to
callback based), since threads can handle this. I just don't know
how to
"grab" the output. I appreciate your time in reading (and answering
this), as I've been googling several hours for this.

There may be more pythonic solution than what I suggest here but this
is what I have done when I needed similar functionality. Basically run
your command in the background and redirect its stdout/err to a temp
file. You may run the command either in the background or in a
separate thread. You can then run the command "tail --retry --
pid=<pid-n+0 -F <output_file>" and grab the output. The tail command
exits once the real command is done.

Or instead use the python subprocess module and read the commands
stdin/out/err from the Popen-object.

Diez
Excellent, thanks!

BTW:
http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

(found this after seeing responses on list)
May 1 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Arvin Portlock | last post by:
I've written a perl cgi script but it's too slow so I need to rewrite it in C++. The perl script passes parameters to a search engine on our local filesystem and processes the results (number of...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
25
by: dennijr | last post by:
ok, shell always used to be easy for me, now its starting to get annoying cause i dont know wats wrong heres the simplist code possible: Private Sub IExplorer_Click() a = Shell("C:\Program...
1
by: pssraju | last post by:
Hi, I am not sure whether I am posting it in right location as i cant see any shell scripting forum here. Below script works perfectly fine from command line, but when I run through browser I am...
2
by: vaskarbasak | last post by:
Hi all, i tried to run Linux command from Java Application. Here i past my code:- package com; public class linux_java { public static void main(String args) {
1
by: jtertin | last post by:
My first inclination would be that this is not possible, but wanted to ask anyway (in case the Shell() call is bi-directional or can return output): If I use VBA to execute a call to the command...
3
by: Max Vit | last post by:
I have come across a strange issue whilst trying to use a shell command call from Access and have spent some time trying to figure this out but can't find the cause as yet. The issue is: I need...
2
by: xtremebass | last post by:
Hi Bytes, in Linux , is it possible to redirect Mysql table output to a file in Linux. i tried it, shows error , but output has displayed in linux prompt when no redirection of file has given(say...
1
by: sala7 | last post by:
i just wanna make a shell in linux by c but i dont good in linux command and in c language so please can any one help me.....?!
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.