How to timeout if a call to an external program takes too longby deivakumar (Initiate)
|on Aug 30, 2007 at 01:42 UTC||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.
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?