edan has asked for the wisdom of the Perl Monks concerning the following question:
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?
|
|---|