I've made some other changes to the code to illustrate what sk and I are talking about.

use constant MAX_ITERATIONS => 100000; use constant MAX_TIME => 6000; use warnings; use strict; my $start_time = time; my $iterations = 0; my $exit_requested = 0; my $handling_request = 0; sub sig_handler { $exit_requested = 1; print "Caught interrupt\n"; exit(3) if !$handling_request; } $SIG{TERM} = \&sig_handler; #init(); while (!$exit_requested && $iterations <= MAX_ITERATIONS && (time - $s +tart_time) < MAX_TIME) { $handling_request = 1; handle_request(); $iterations++; $handling_request = 0; handlyn(); } exit(0); #sub init { return 1; } sub handle_request { handlyn(); print ("iterations: $iterations\n"); } sub handlyn { print $handling_request ? "" : "NOT ", "handling\n"; sle +ep 3; }

The handlyn() gives me time to send a kill to the process which I'm running from the perl debugger. I then issue a "c 13" in the debugger and wait for the "NOT handling" to be printed, send a kill and then step into the script using "s". You'll see that when $handling_request == 1, the script will exit from line 15. When $handling_request == 0, the script exits from line 29. Which I think is how you want it?

The question is - how does your handle_request work? Is it able to deal with being interrupted only to have execution return to some arbitrary line in the subroutine? What I've done here is to control where the interrupt will occur by giving myself time to send the interrupt at that particular line. But, that isn't how signals generally work in the wild...

Update: corrected link.


In reply to Re^3: Controlling a Perl Daemon with Signals by jch341277
in thread Controlling a Perl Daemon with Signals by eastcoastcoder

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.