Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Progress Bar Between Multiple System Calls

by monkfan (Curate)
on May 05, 2007 at 08:26 UTC ( #613697=perlquestion: print w/ replies, xml ) Need Help??

Dear all,
AFAIK, progress bar can only be applied under loops. For example we can use Term::ProgressBar or Smart::Comments. But is there away to do it for system call like this:
#!/usr/bin/perl system("code1.out param1 > output.txt"); system("code2.out param2 output.txt > output_final.txt");
How can I show Progress Bar between first (code1) system call and the second (code2) system call? Especially we don't know how long each system call will take.

Preferrably not using external CPAN module (except pre-installed module).


Comment on Progress Bar Between Multiple System Calls
Download Code
Re: Progress Bar Between Multiple System Calls
by GrandFather (Cardinal) on May 05, 2007 at 08:42 UTC

    If you mean "How do I show progress of the system call" the general answer pretty much is "You can't". To show a sensible progress bar you have to have some way to provide a progress update that indicates how far through the process is. In the general case for a system call you can't do that. At best you could estimate the time that may be required and show a progress bar based on time (or similarly for an external measure of the progress - target file size for example) in a forked process. That's a pretty hairy and unsatisfactory solution though.

    DWIM is Perl's answer to Gödel
Re: Progress Bar Between Multiple System Calls
by BrowserUk (Pope) on May 05, 2007 at 11:46 UTC

    If you just want to show that progress is being made, rather how far things have got, monitoring the size of the output file is easy using a thread:

    Added a sleep to the display loop to prevent it from running away with the cpu. Using Win32::Sleep( 500 ); or selectundef, undef, undef, 0.5 might be better?

    #! perl -slw use strict; use threads; use threads::shared; my $outfile = 'output.txt'; unlink $outfile ; my $done:shared = 0; async{ system("dir /s > $outfile"); $done =1; }; sleep 1 until -e $outfile; printf STDERR "\r%d\t", -s( $outfile ) and sleep 1 until $done; print STDERR 'Output complete';

    If you can estimate the size of the output file--for example in your second command, there may be some rough correspondance between the size of the output from the first and that from the second--then you can easily convert the raw numbers into an estimated percentage. If your percentage runs over by a few percent and finishes at 105%, you'll be forgiven provided that you a) indicate that it is an estimate; b) it proves reasonably accurate.

    Alternatively, add 10% to your final size estimate. When the command finishes 'early' at say ~90%, no one will complain :)

    Wrapping that up into a function that takes the command, the path of the output file and an estimate of the final size is left as an exercise.

    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Progress Bar Between Multiple System Calls
by ambrus (Abbot) on May 05, 2007 at 16:07 UTC
    print "[ ] 0%\r"; system("code1.out param1 > output.txt"); print "[############ ] 50%\r"; system("code2.out param2 output.txt > output_final.txt"); print "[########################] 100%\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://613697]
Approved by GrandFather
Front-paged by neversaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-07-13 01:56 GMT
Find Nodes?
    Voting Booth?

    When choosing user names for websites, I prefer to use:

    Results (245 votes), past polls