2

I have Linux command that is running in Python.

roc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', hostname, '/home/zurelsoft/test'], 
                                    stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
print roc

This print the command processing only when it finishes execution. But, I want the output of the command as it is happening and stops when the command is fully executed. How it can be done?

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
user1881957
  • 3,258
  • 6
  • 34
  • 42

2 Answers2

3

you can check out Select and Select Example

Python’s select() function is a direct interface to the underlying operating system implementation. It monitors sockets, open files, and pipes (anything with a fileno() method that returns a valid file descriptor) until they become readable or writable, or a communication error occurs.

select() makes it easier to monitor multiple connections at the same time, and is more efficient than writing a polling loop in Python using socket timeouts, because the monitoring happens in the operating system network layer, instead of the interpreter.

If this does not help you can also look at

Persistent python subprocess

How can I read all availably data from subprocess.Popen.stdout (non blocking)?

Community
  • 1
  • 1
avasal
  • 14,350
  • 4
  • 31
  • 47
  • Thank you for your answer. I want to store the output of the command as it is happening in the variable and send it to javascript as json. But I suppose stdin.write() outputs in the terminal itself. Can it be stored in a variable and can be passed in javascript? Thanks – user1881957 Dec 19 '12 at 06:12
  • @user1881957: you can dump it in a file, and read it after completion, but if you want to use a variable for this well as per my understanding you can't since you can't append the output to a variable, you can only reassign it, which you don't want (it will assign the new output and overwrite the output stored previously ) – avasal Dec 19 '12 at 07:26
  • I am trying to show the progress bar using those data's so it's not point to read it after completion. I need a way to read the progress as it is happening. – user1881957 Dec 19 '12 at 09:46
  • i would suggest you create a separate process to monitor the progress of the output, not the same one, the new process will also be responsible for showing a graphical way to show the progress, the new process will get initiated as soon a subprocess cmd is executed and will be alive till progress is 100% or user forcefully kills it – avasal Dec 19 '12 at 10:51
  • I was thinking the same. Is there some tutorial for this? It should be easy to use in JavaScript too. – user1881957 Dec 19 '12 at 11:37
  • you can probably have a look at http://greenash.net.au/thoughts/2011/06/thread-progress-monitoring-in-python/, http://www.parallelpython.com/component/option,com_smf/Itemid,1/topic,213.0/prev_next,next – avasal Dec 19 '12 at 11:40
0

This should work from commandline if you call it via subprocess.Popen as you do, but pass a pipe to sys.stdout by changing:

 stdout=sys.stdout

Passing a pipe to an open, writable file object saves the output.

out = open("output.tmp","w")
subprocess.Popen(["ls","-R"],stdout = out)
out = open("output.tmp","r")
output = out.readlines()
Schuh
  • 1,045
  • 5
  • 9