Solved

How to display a progress bar of remote linux process

Posted on 2011-09-30
3
497 Views
Last Modified: 2012-05-12
I am looking for a way to generate a progress bar for a process executing on a remote linux machine (through ssh). The process on the remote machine is a g++ compilation of a big C++ project.

A lot of output text is produced by the compilation process. I imagine to generate the progress bar by displaying (in the shell on the local machine) a "#" each time a line starting with "Making install in" comes out from the compilation process on the remote machine (I know how many such lines it will be in total, so I know how long the progress bar will be).

The remote process is started from a python script on the local machine, and I have currently come so far:

cmd="ssh "+rhost+" \"cd "+r_build_dir+" ; make -j install | grep 'Making install in'\""
os.system(cmd)
# perhaps I could also have used: commands.getoutput(cmd) instead of os.system(cmd)

This kind of does what I want, but the whole point is lost because all the lines come out at once when the remote compilation process is done!, e.g.:

Making install in ProjectRoot
Making install in ProjectRoot/Project1
Making install in ProjectRoot/Project2
Making install in ProjectRoot/Project3

Of course the lines should be displayed one by one as the compilation progresses. Ideally I don't even want see those lines coming, but a growing progress bar.

The solution is probably related to flushing stdout and ssh. You experts probably have a "one-liner" to do the job!

Thanks!
0
Comment
Question by:Phazz
3 Comments
 
LVL 9

Expert Comment

by:parparov
ID: 36894867
You may want not to pipe the output into grep, but redirect it into the file, and fork a parallel process that would access the remote machine, read the redirected file and output progress as it goes.

0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 500 total points
ID: 36900037
You probably want to use the subprocess module and popen to catch the output as it comes in.  The os.system is just going to execute the command.

import re
from subprocess import Popen,PIPE

lookfor = re.compile(r'Making install in')

rhost = "somehost"
cmd = "ssh "+rhost+" \"cd "+r_build_dir+" ; make -j install"

build = Popen(cmd, stdout=PIPE, shell=True, universal_newlines = True, bufsize=1024 )

count = 0
while build.poll() == None:
        line = build.stdout.readline(1024).strip()
        if (lookfor.match(line)):
                count = count + 1
                print "%04d" % count

Open in new window

0
 

Author Closing Comment

by:Phazz
ID: 36908296
Excellent, brilliant! Thanks a lot!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VMWare 6 crashing 14 96
PHP Apache application gives "Forbidden" message 5 43
PC upgrade to Linux Mint 7 64
Path to Python 9 104
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now