Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

How do I trap $SIG{INT} ( sigint aka Ctrl^c )?

by crazyinsomniac (Prior)
on Jul 01, 2001 at 13:15 UTC ( #93004=categorized question: print w/replies, xml ) Need Help??
Contributed by crazyinsomniac on Jul 01, 2001 at 13:15 UTC
Q&A  > programs and processes


I've got a commandline script, and I wanted to do some stuff before ending the program if somebody hit Ctrl^C.

I'm using ActivePerl and I've tried:

#!/usr/bin/perl -w use strict; $SIG{INT}=\&myhand; sub myhand { print "\n caught $SIG{INT}",@_,"\n"; } print "program started \n"; while(1){sleep 1;}
I need to hit Ctrl^c twice to kill the program.
If I add exit; or die; at the end of myhand, perl crashes (PERL caused an exception c0000026H in module KERNEL32.DLL at 015f:bff88510.).

How do I fix this?
I've read perlman:lib:sigtrap and I've also tried:

use sigtrap 'handler' => \&myhand, 'INT'; sub myhand { print "\n caught $SIG{INT}",@_,"\n"; } print "program started \n"; while(1){sleep 1;}
which should be the same as my first example, but it also doesn't work (same errors as before).

Answer: How do I trap $SIG{INT} ( sigint aka Ctrl^c )?
contributed by crazyinsomniac

Basically you need to kill yourself (the process).

#!/usr/bin/perl -w use sigtrap 'handler' => \&myhand, 'INT'; # equivalent to $SIG{INT}=\&myhand; # use strict; # kill 6 doesn't work under strict under IndigoPerl for some reason sub myhand { print "\n caught $SIG{INT}",@_,"\n"; kill 6, $$; # ABRT = 6 # $$ is the pid of the current process } print "program started \n"; while(1) { select(undef,undef,undef,0.25);}

Also check out perlvar(for $$), perlipc and How do I trap control characters/signals?.
kill 6 is the equivalent of kill ABRT (thanks Vynce).

Answer: How do I trap $SIG{INT} ( sigint aka Ctrl^c )?
contributed by Anonymous Monk

Instead of killing yourself, as suggested by crazyinsomniac, you could also try just exiting. This avoids the shell's response of 'aborted' when the program is killed. I.e.:

sub myhand
  print "\n caught $SIG{INT}",@_,"\n";

-Josh O-

Answer: How do I trap $SIG{INT} ( sigint aka Ctrl^c )?
contributed by thayer

Well, exit or die would work under unix, so switch to Linux and your life will be much better ;^)

However, if that's not an option, then any OS will let you do something like:

$Done = 0; sub myhand { print "Caught SIGINT\n"; $Done = 1; } ... while(1) { sleep(1); $Done && die "Time to go"; } ...
I'd also like to warn you that if you use system, or sleep you're signal handlers may get reset for INT and ALRM respectively, so what's in the run loop may contribute to your woes... I'm not sure what Windows side-effects to look for...


Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [ambrus]: Corion: some of these stupid syntax highlighters assume that too. just look at the table in http://perldoc. functions/pack. html for example.
    [haukex]: ..."yet" ;-) I haven't had to deal with Dist::Zilla yet but I've heard about how it's a big setup
    [ambrus]: I really don't like automagic stuff. I'm happy when computers do exactly what I tell them, even if that means they sometimes do the wrong thing.
    [ambrus]: And I don't much like syntax highlighters. If you need a syntax highlighter to understand your code, then your code is written unclear.
    [ambrus]: And if you need a syntax highlighter to color parenthesis green and numbers black and letters blue, then you're using the wrong font.
    [ambrus]: I have to tolerate syntax highlighters when other people use them, but I don't use them myself. And sorry for the rant.

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (14)
    As of 2017-02-27 12:45 GMT
    Find Nodes?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?

      Results (385 votes). Check out past polls.