I have a daemonized server (Linux/Perl 5.8.8) and I would like to perform a task every few minutes. Using a SIGALRM handler seems to not work because it causes accept() to return. So I'm trying to fork off a daemon process that just sleeps and sends SIGUSR1 to my process. However, my server dies the first time the signal handler is run. Here's a simple reduced case with the same behavior:
The server:
#!/usr/bin/perl
use IO::Socket;
#defined(my $pid=fork) or die "Can't fork: $!";
#exit if $pid;
#setsid or die "Can't start a new session: $!";
my $sock = new IO::Socket::INET (
"Proto" => "tcp",
"LocalPort" => 8819,
"Reuse" => 1,
"Listen" => 1
) or die "Server croaked";
$SIG{'USR1'} = \&sigHandler;
system("./sleep.pl $$");
while($client = $sock->accept())
{
}
sub sigHandler
{
print "SIGNAL HANDLER\n";
}
The forked process (sleep.pl):
#!/usr/bin/perl
$ppid = $ARGV[0];
print "$ppid";
defined(my $pid=fork) or die "Can't fork: $!";
exit if $pid;
setsid or die "Can't start a new session: $!";
while(1)
{
sleep(10);
print "DISPATCHING SIGNAL\n";
kill(SIGUSR1, $ppid);
}
If you know why my server is dying or you know A Much Better way to accomplish the same objective, I'd be very appreciative.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.