Monks,

I have a daemon that runs on a machine, with several sockets open to other nodes. We have a problem that when the system goes down (via reboot, shutdown, or the like), the socket gets hung up on the other side - I have been told this is a known problem with tcp sockets...

Anyway, the program basically runs in a infinite loop, looping on an IO::Poll call waiting for events on the sockets. I have installed sig handlers for INT, KILL, and TERM. The sig_handler basically sets a global variable that indicates the signal that was caught, and on every loop I check the global, and exit the loop if I got a signal. This seems to work when I send a signal via kill -TERM.

What doesn't work is when I reboot or shutdown the machine - my program doesn't seem to get the signal - the man page for shutdown and reboot seem to indicate that all processes are sent the TERM signal to allow them to shut down, etc., but I don't seem to be able to trap it.

Oh yeah, I'm running Linux (kernel rev 2.4.19)

(Apologies that it's a bit more OS'y than Perl'y)

The folllowing code illustrates what I'm doing:

#!/usr/bin/perl my $sig; print "my pid: $$\n"; $SIG{TERM} = \&handler; while(1) { if ($sig) { last; } select(undef, undef, undef, .2); # sleep for .2 sec } # clean up here `echo $sig > /tmp/sig.out`; # to check after reboot if it caught it exit; sub handler { $sig = shift; }

Any thoughts?

--
3dan

In reply to (slightly OT) catching SIGTERM when system goes down by edan

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.