Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

( #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
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (3)
    As of 2015-11-27 04:28 GMT
    Find Nodes?
      Voting Booth?

      What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

      Results (717 votes), past polls