Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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 > tester.pl 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

Comment on INT signal kills process run with expect, when $SIG{'INT'} is set
Select or Download Code
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.

Re: INT signal kills process run with expect, when $SIG{'INT'} is set
by salva (Monsignor) 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://889536]
Approved by GrandFather
Front-paged by salva
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2014-09-19 21:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (147 votes), past polls