[In reply to updated question, where the OP attempts to use fork.]

Close. You'll never reach to waitpid. Also, system(...); exit(); is the same as exec(...);

I believe the code below is a robust (allows for missed polls or missed twitches), reusable (a user-provided function indicates when to stop polling) and fancy (seperate timers for polling and twitching) solution. (Untested)

use POSIX qw( WNOHANG ); use Time::HiRes qw( time sleep ); sub min { $_[0] < $_[1] ? $_[0] : $_[1] } sub max { $_[0] > $_[1] ? $_[0] : $_[1] } sub twirling_baton { my ($twitch_interval, $poll_interval, $poll_func) = @_; my $af = ($| = 1); my $tcount = 0; my @baton = qw( | / - \ ); print($baton[0]); my $time = time; my $twitch_time = $time + $twitch_interval; my $poll_time = $time + $poll_interval; for (;;) { sleep(max(min($twitch_time, $poll_time) - $time, 0.250)); # Sleep is not precise. $time = time; if ($time >= $poll_time) { last if &$poll_func(); $time = time; # In case the call is slow. $poll_time = $time + $poll_interval; } if ($time >= $twict_time) { $tcount = ($tcount + 1) % @baton; print("\b", $baton[$tcount]); $twitch_time = $time + $twitch_interval; } } $| = $af; } $pid = fork; if (not defined $pid) { die("Unable to create child process: $!\n"); } if (not $pid) { exec("$program < $temp_file"); die("Unable to launch application: $!\n"); } twirling_baton( 0.250, # Twitch every 500ms 0.500, # Poll every 500ms sub { waitpid($pid, WNOHANG) != 0 }, );

Update: Oops, I used : instead of ? in min and max. Fixed.


In reply to Re: Executing a shell script with progress indicator by ikegami
in thread Executing a shell script with progress indicator by axl163

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.