Hello everyone, I have a script (> 400 Lines) in wich I have implemented a timeout routine using SIG{ALRM}. The code used is the following:
$cmd="myCommand"; # Timeout evaluation: eval { # Set action to be done when alarm occurs: local $SIG{ALRM} = sub { die "ALARM_COMMAND_TIMEOUT" }; # Schedule alarm in $CMD_TIMEOUT seconds: alarm($CMD_TIMEOUT); ### execute the command that can block $childPID = open(PIPE, "-|"); ### Unable to create child process unless(defined $childPID) { close(PIPE); die "ERROR_FORK_PROCESS: $!\n"; } ### Child Process Created ... execute command unless($childPID) { exec($cmd) or die "ERROR_OPEN_PIPE : $!\n"; ### Unable to create child process } wait; alarm(0); close(PIPE); }; alarm(0);
During the test period, I noticed something strange. When using this code, my whole script is 'respawned' instead of the single command I want to check for timeout. Maybe the code is wrong. All I need is to run a single command and wait for the result withing a given timeout. If the command takes too long I want to kill it. The script is running on AIX 4.3.3 & 5.2. Perl version is 5.8. Any suggestions ? Thank you very much.

In reply to Timeout with SIG{ALRM} not working as expected by MorgothSauron

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.