Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am working on a script which execute different list of commands one by one. For example,
my % commands = ( CMD1 => process 1, CMD2 => process 2, CMD3 => process 3, ); foreach my $cmd(keys %commands) { `$commands{$cmd}`; if($?) { print ¨ Error: $commands{$cmd}\n¨; } }

Now my question is, in above execution is there any way to print below statement after every one minute of one of the process took more than a minute,


print ¨Still executing: $command{$cmd}\n¨;

Cheers,

Replies are listed 'Best First'.
Re: execute command as well as prompt user
by BrowserUk (Patriarch) on Aug 31, 2010 at 11:18 UTC

    Here's one way (BTW. Using backticks and ignoring the output doesn't make a lot of sense):

    use threads; use threads::shared; my % commands = ( CMD1 => process 1, CMD2 => process 2, CMD3 => process 3, ); foreach my $cmd(keys %commands) { my $done :shared = 0; async { `$commands{$cmd}`; if($?) { print ¨ Error: $commands{$cmd}\n¨; } $done = 1; }->detach; until( $done ) { for( 1 .. 60 ) { sleep 1; last if $done; } print ¨Still executing: $command{$cmd}\n¨ } }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: execute command as well as prompt user
by dasgar (Priest) on Aug 31, 2010 at 15:12 UTC

    I was thinking about threads, but I doubt I would have been able to provide example code like BrowserUK did.

    Also, I agree with BrowserUK's comment about backticks. If you're not interested in the STDOUT of the commands or need the exit code the commands, I think system may work better.

    However, if you need to capture both STDOUT and STDERR, I believe that IPC::Open3 may be the better route to go.