I wrote a CLI programm running on windows 7 (64 Bit). I would like to call some cleanup code whenever the programm is terminated through closing the window (someone clicks the [x] in the upper right corner). Ideally the cleanup code should be run for any abnormal termination for which a handler can be registered.
Perl Versions used for testing:The actual programm is compiled using pp. For my tests i called the perl script using portableshell.bat, and then closed the window using the [x] in the upper right corner. Until now i failed to find a way to trigger one of my handlers. I tried a number of things, and my google and perlmonks search skills have failed me so far. As such i come here, in the hope that someone might have already solved this problem. Example code for some failed attempts:strawberry-perl-5.18.1.1-32bit-portable strawberry-perl-5.28.0.1-64bit-portable
After the programm is terminated through closing the window, the log is still empty.use strict; use warnings; use FindBin; use File::Spec; my $log_fn = File::Spec->catfile($FindBin::Bin, "cleanup_handler_log.t +xt"); my $LOG; open $LOG, '>>', $log_fn or die "Failed to open log '$log_fn' for appe +nding ($!)\n"; binmode $LOG; $LOG->autoflush(1); # disable buffering # Intent: register a cleanup handler, which is called when the process + is terminated # because someone closed the window using [x] in Windows. # # Random examples of what does not work: sub END { print $LOG "END() called. Cleaning up...\n"; } sub DESTROY { print $LOG "DESTROY() called. Cleaning up...\n"; } foreach my $s (qw/INT TERM HUP QUIT ABRT KILL CHLD STOP/) { $SIG{$s} = sub { print $LOG "SIG $s received! Cleaning up ...\n"; exit 0; }; } my $sleep_time = 60 * 60 * 24; # 1d print "Sleeping $sleep_time seconds ...\n"; sleep $sleep_time; print "Print finishing normally (sleep $sleep_time completed)\n";
In reply to Window Close / Process Termination Handler on Windows by rminner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |