Hello Fellow Monks

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:
strawberry-perl-5.18.1.1-32bit-portable strawberry-perl-5.28.0.1-64bit-portable
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:
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";
After the programm is terminated through closing the window, the log is still empty.

In reply to Window Close / Process Termination Handler on Windows by rminner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.