=sjs= has asked for the wisdom of the Perl Monks concerning the following question:

Hi All

I'm having problems understanding how SIG works on UNIX I guess.

I have a script that looks something like:

$SIG{INT} = sub {$Abort++}; while (1) { if($Abort) exit(0); system(do something); }
As far as I can tell, my signal handeler is never called, so I assume the forked process under the system call is getting the signal -- it does exit. How do I get this passed up to the parent?

Thanks
Steve S.

Edited by Chady -- added code tags, minor formatting.

Replies are listed 'Best First'.
Re: SIG and system
by Roy Johnson (Monsignor) on Dec 06, 2004 at 18:49 UTC
    According to perldoc -f system,
    Because "system" and backticks block "SIGINT" and "SIGQUIT", killing the program they're running doesn't actually interrupt your program.
    However, I find that I get different results using backticks compared to system:
    use strict; use warnings; my $Abort=0; $SIG{INT} = sub { ++$Abort }; for (1..10) { print "Iteration $_: Abort = $Abort\n"; `sleep 5`; }
    $Abort will increment if I use backticks, not if I use system.

    Caution: Contents may have been coded under pressure.
Re: SIG and system
by bluto (Curate) on Dec 06, 2004 at 18:53 UTC
    The easiest way to do this in this particular case is not to catch a signal, but to check the lower eight bits of the exit code from system. It will usually indicate the signal number the child received. I say "usually" since if you are actually launching a shell, esp. with pipelineing, they don't always behave nicely (i.e. make sure you test).
Re: SIG and system
by =sjs= (Initiate) on Dec 06, 2004 at 21:36 UTC
    Thanks all for the suggestions and thanks for formatting previous message. This was exactly what I needed.
    Steve S.