in reply to SIG{ALRM} to help kill external program

Alarm handlers do not survive a fork (which hides under the covers of backticks), this is not Perl specific, it is a feature of UNIX. So you need to use fork, do the signal handling in the child, then exec your external application. See also Proc::Background which includes timeout handling (it uses sleep for the timeout).
  • Comment on Re: SIG{ALRM} to help kill external program

Replies are listed 'Best First'.
Re^2: SIG{ALRM} to help kill external program
by roboticus (Chancellor) on Oct 13, 2010 at 16:12 UTC

    cdarke:

    Can you point to some documentation? I did a quick search (not very thorough), and haven't found any evidence that the alarm handlers don't survive a fork. The OpenGroup alarm documentation clearly states that pending alarm signals are cleared for the child, and their fork docs state that the child process will have its alarm cleared and any pending alarm reset. So I don't see why the parent using alarm & kill wouldn't work.

    I don't have my hands on a Unix box at the moment, but under windows, I get this:

    301058@LOU-PC0288 /Work/METABASE $ perl forktest.pl nofork: ALARM! nofork: done... 301058@LOU-PC0288 /Work/METABASE $ perl forktest.pl a PID(2560): ALARM! PID(0): QUITTING! PID(2560): done... 301058@LOU-PC0288 /Work/METABASE $ cat forktest.pl use strict; use warnings; my $prefix="nofork"; my $PID; $SIG{INT} = sub { die "$prefix: QUITTING!\n\n"; }; $SIG{ALRM} = sub { print "$prefix: ALARM!\n\n"; kill 2,$PID if $PID; } +; alarm(5); if (@ARGV) { # with any argument we fork, o/w we don't $PID = fork; $prefix = "PID($PID)"; } # Wait long enough for alarm to trigger my $t = time; while ($t + 10 > time) { # doze } print "$prefix: done...\n\n"; alarm(0); 301058@LOU-PC0288 /Work/METABASE $

    ...roboticus