Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

How to timeout if a call to an external program takes too long

by deivakumar (Initiate)
on Aug 30, 2007 at 01:42 UTC ( #635971=perlquestion: print w/replies, xml ) Need Help??
deivakumar has asked for the wisdom of the Perl Monks concerning the following question:

I am writing some test scripts for a small project that I am working on. Basically, the project involves the running of a Command Line Interface (CLI) tool (my creation) and the test needs to process and verify that the output is the expected result. Simple.

However, there are circumstances where the CLI could hang or wait on a response for a very long time due to the CLI interacting with hardware and the hardware not responding due to some internal hardware error.

$cmd = "MyProgram.exe"; $output = `$cmd`; # The problem I am facing at this point is, if # "MyProgram.exe" hangs, or takes a long time to # respond, the script will not be able to process # the output or proceed to run other tests.

I was wondering if there was any way where after the call to my external program is made, and given a fixed waiting period, a timeout or termination of my external program can be issued? After the timeout or termination the CLI's process, I intend to continue running other scripts, somewhat like an automated test.

I have tried to use alarm() previously but was not able to get it to work.

I found that since I was feeding the output of the external program to a variable (which I need for processing), the control does not return to the script until the external program exits. So, if the external program goes into an infinite loop, the alarm raised would not be able to be handled because the external program will still be hogging the control.

Anyone able to help?

Replies are listed 'Best First'.
Re: How to timeout if a call to an external program takes too long
by BrowserUk (Pope) on Aug 30, 2007 at 01:54 UTC

      That is just a perfect solution for me.

      Thanks a bunch to all who responded.

Re: How to timeout if a call to an external program takes too long
by kyle (Abbot) on Aug 30, 2007 at 02:07 UTC

    Can we see the non-working code that uses alarm? This is a textbook example of when to use alarm.

    Here, the alarm is triggered, and then processing continues:

    alarm 5; $SIG{ALRM} = sub { print "alarm!\n" }; my $yawn = `sleep 10 ; echo "yawn"`; print "yawn? $yawn\n"; __END__ alarm! yawn? yawn

    In this case (adapted from the example in the alarm documentation), the alarm is triggered, and processing is halted:

    my $yawn; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 5; $yawn = `sleep 10 ; echo "yawn"`; alarm 0; }; print $@ ? 'Timed out.' : 'Finished.'; print "\n"; print "yawn? $yawn\n"; __END__ Timed out. yawn?

      Have you tried that when safe signals are in force?

      It certainly doesn't work on the OPs platform (win32 going by his use of MyProgram.exe).

      I believe that it also doesn't work on *nix platforms since 5.8.0 unless you explicitly disable safe signals .

      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.

        I tried both of my code snippets before posting. My platform is Perl 5.8 on OS X:

        kyle@sulaco:~ $ perl -v | head -6 This is perl, v5.8.6 built for darwin-thread-multi-2level (with 3 registered patches, see perl -V for more detail) Copyright 1987-2004, Larry Wall kyle@sulaco:~ $ uname -a Darwin sulaco 8.10.0 Darwin Kernel Version 8.10.0: Wed May 23 16:50:59 + PDT 2007; root:xnu-792.21.3~1/RELEASE_PPC Power Macintosh powerpc

        I did nothing to disable safe signals. The only part of what I actually executed that I left out of what I posted was my usual "use strict;use warnings;" (and I also cut out the "Use of uninitialized value..." warning that resulted).

        I don't have a Windows machine to test on.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://635971]
Approved by tye
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2018-05-25 17:41 GMT
Find Nodes?
    Voting Booth?