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

Hi Monks,

I have a setup script(A) which triggers one of my scripts(B) in which I invoke another script(C) using qx//. The invoked script (C) might take hours for completion and the output of the same is captured in a variable to error checking and parsing the results.

$exec_status = `$cmd 2>&1`;

But the problem is that, the setup script(A) actually looks for some messages printed on the console or else it will timeout after waiting for a fixed time. So is there any way I can use in my script (B) which actually does not wait for the script (C)'s completion and print something on the STDOUT periodically like "Waiting for $timout seconds..".

When I use backtick, the script B waits till the script C's completeion which is a very long time. The use of system() works for me which prints to STDOUT, but I cannot parse the output of script C.

So basically I just want to combine the functions of backtic and system() which is not possible as far as I know. If I redirect output to a file how will I monitor that the execution is completed and EOF has reached if I use the below method.

$timeout=1000; open (FP, "scriptC.pl |") or die 'Error'; while (<FP>) { $output .= $_; sleep 30; $timeout -=30; print 'Waiting for execution to finish'. $timeout; }
Hope you can pour some insights to this.

Replies are listed 'Best First'.
Re: Capturing output of qx// without waiting for completion
by vinoth.ree (Monsignor) on Feb 24, 2014 at 08:00 UTC
Re: Capturing output of qx// without waiting for completion
by kcott (Archbishop) on Feb 24, 2014 at 08:14 UTC

    G'day ajose,

    I'm unclear on how A "triggers" B, B "invokes" C, what type of scripts those are and whether any of that matters.

    However, your requirement for capturing output and periodic "Waiting ..." messages when no output has been found, seems very similar to a question from a few weeks ago.

    Take a look at my initial response to Check for a new line, the follow-up questions, and then my subsequent response.

    My solution (using alarm) has similarities to what you appear to attempting with your "$timeout=1000; ..." code. You may be able to modify it directly to do what you want or use it as the basis for a new solution.

    -- Ken

      I am also not sure how A triggers B. As far as I know that A is written in python and it invokes the script(B) we pass on. My script B just lies between A and C in order to create environment and result parsing for script C.

      I am currently looking into your suggestions and will update whether it works or not.