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?


In reply to How to timeout if a call to an external program takes too long by deivakumar

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.