from perlipc:
Not all platforms automatically reinstall their (native) signal handlers after a signal delivery. This means that the handler works only the first time the signal is sent. The solution to this problem is to use "POSIX" signal handlers if available, their behaviour is well-defined.
So to rework your example with the one from perlipc:
!/usr/bin/perl -w
use POSIX ();
use FindBin ();
use File::Basename ();
use File::Spec::Functions;
$|=1;
# make the daemon cross-platform, so exec always calls the script
# itself with the right path, no matter how the script was invoked.
my $script = File::Basename::basename($0);
my $SELF = catfile $FindBin::Bin, $script;
# POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new('sigHUP_handler',
$sigset,
&POSIX::SA_NODEFER);
POSIX::sigaction(&POSIX::SIGINT, $action);
sub sigHUP_handler {
print "got SIGINT\n";
exec($SELF) or die "Couldn't restart: $!\n";
}
code();
sub code {
my $c = 0;
while (++$c) {
sleep 2;
print "$$ - $c\n";
}
}
-derby
Update: Sorry forgot to tell you. I have the same platform as you (Mac OS X 10.3.8, perl v5.8.1-RC3)
Update: And a lot more like your original code:
#!/usr/bin/perl
use POSIX();
$|=1;
$sub = sub { exec "$0" };
# POSIX unmasks the sigprocmask properly
my $sigset = POSIX::SigSet->new();
my $action = POSIX::SigAction->new(
$sub, $sigset, &POSIX::SA_NODEFER );
POSIX::sigaction(&POSIX::SIGINT, $action);
print (++$counter), sleep 1 while 1
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|