in reply to How do I dump data to file on hard exit?

Hi,

You can setup a handler for SIGINT

$SIG{INT} = \&ctrlc_exit;

ctrlc_exit{
print LOGFILE "exiting by control c\n";
die "program exiting elegently";
close LOGFILE;
}


hope this helps
Displeaser
  • Comment on Re: How do I dump data to file on hard exit?

Replies are listed 'Best First'.
Re^2: How do I dump data to file on hard exit?
by displeaser (Hermit) on Mar 31, 2006 at 13:54 UTC
    DOH!!!

    Closed the file after dieing, ah well you know what i mean.
    BTW, this will only get ctrl c it wont do a server reboot.

    Displeaser
      The process might get a TERM signal on a reboot so set up a handler for that as well.

      $SIG{TERM} = ...

      Cheers,

      JohnGG

      BTW, this will only get ctrl c it wont do a server reboot.

      Most (all?) Unix OSs will send a TERM signal to every process running when shutting down, and a KILL one if they do not terminate after a while.

Re^2: How do I dump data to file on hard exit?
by bart (Canon) on Apr 01, 2006 at 08:50 UTC
    Please set
    $SIG{INT} = 'IGNORE';
    as the first thing in your handler, or else the sub will recurse the first time someone presses ctrl-C while in this sub.

    And, Like you remarked yourself, you still have to actually exit or die at the of the sub, or it'll just resume where it left off, afterwards.

      Thanks, I used the signal handlers. Also the autoflush negated the need to open and close the file each iteration.