use strict; use warnings; use FindBin; use File::Spec; my $log_fn = File::Spec->catfile($FindBin::Bin, "cleanup_handler_log.txt"); my $LOG; open $LOG, '>>', $log_fn or die "Failed to open log '$log_fn' for appending ($!)\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";