Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

INT signal kills process run with expect, when $SIG{'INT'} is set

by ISAI student (Scribe)
on Feb 22, 2011 at 08:26 UTC ( #889536=perlquestion: print w/replies, xml ) Need Help??
ISAI student has asked for the wisdom of the Perl Monks concerning the following question:

Hello all. I am trying to ramp up a new utility, which accepts a ^C from the user, outputs running processes (ran by the script), and asks for the user prompt. I have tried something very simple. I spawn a sleep process, via Expect, and every time that I hit ^C, I expect PERL to output a message. The code is:
require Expect; use strict; $SIG{'INT'}=sub {print "Al Bundy rules!\n";}; $SIG{CHLD}='IGNORE'; my $proc=Expect->spawn("sleep 10"); unless ($proc->expect(10,"Mommy")) { print "Stop selling women shoes\n"; }

Whe I hit ^c once, I get:

liord@analog2 238 > Al Bundy rules! Stop selling women shoes
And the script exits. I have expected the scipt NOT to exit after one ^c, but after n presses (or 10 seconds). What am I doing wrong? PERL version is: This is perl, v5.8.8 built for x86_64-linux-thread-multi

Replies are listed 'Best First'.
Re: INT signal kills process run with expect, when $SIG{'INT'} is set
by salva (Abbot) on Feb 22, 2011 at 09:34 UTC
    That happens because pressing CTRL-C at the terminal does not send the kill signal just to the process but to the full process group.

    The workaround is to get setpgrp called between the fork and exec calls used internally to spawn the process... and that probably means writing your own spawn method.

Re: INT signal kills process run with expect, when $SIG{'INT'} is set
by ISAI student (Scribe) on Feb 22, 2011 at 08:34 UTC
    It's actually kills even w/o Expect. Code below is killed with ^c too:
    require Expect; use strict; $SIG{'INT'}=sub {print "Al Bundy rules!\n";}; sleep 10000;
    The prcess lasts until 1st ^c.
      On many platforms sleep will exit on any signal, including SIGINT. You need to place it in a loop. For example:
      use warnings; use strict; $SIG{'INT'}=sub {print "Clive Darke rules!\n";}; while (1) { sleep 10000; }

      It doesn't kill the process, handled signals interrupt sleep. If they didn't, the handler wouldn't get to run. See Re: fork() doesn't care about my sleep()? for how to make sleep uninterruptible.

      The OP didn't (visibly) use a sleep in the process that has a signal handler, so this isn't (likely to be) the OP's problem.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://889536]
Approved by GrandFather
Front-paged by salva
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2017-05-26 05:50 GMT
Find Nodes?
    Voting Booth?