Why can't I trap ctrl-C in a perl script?
I am trying to trap ctrl-C, so the perl script can do an orderly shutdown instead of just breaking, leaving caches unflushed and work unsaved. Here's my code:
$SIG{'INT'} = 'shutdown'; $SIG{'QUIT'} = 'shutdown';
(the bulk of my program here)
sub shutdown {
warn "Flushing caches and shutting down now\n";
&flushcache;
$SIG{'INT'} = 'DEFAULT'; $SIG{'QUIT'} = 'DEFAULT';
$shutdownnow = 1;
exit;
}
When I hit ctrl-C, it ignores ctrl-C and just keeps running. I have to ctrl-Z then go 'kill %1' which of course does not cause it to shut down gently.
Mac OS X (10.5.8)