Getting program to restart (HUP) more than once 
Author Message
 Getting program to restart (HUP) more than once

I would be grateful if someone could help with this. I am writing a program that
installs a signal handler or two. One signal I want to catch is a 'hang-up'
(HUP) signal. The program does catch the HUP and restart correctly, but will
only do this once. It then ignores any subsequent HUP.

I assume this is something to do with the 'exec' to restart. Is the handler not
getting re-installed after the exec of the program? This is a test program that
exhibits this behaviour (tried on linux and irix) ;

#!/usr/bin/perl -w

my $P = '/ext1/people/alastair/dev/perl/daemon/ddd';

$SIG{HUP} = \&hup_handler;

sub hup_handler {
        $SIG{HUP} = \&hup_handler;
        print "Restart ...\n";
        exec $P or die "Cannot exec: $!";

Quote:
}

my $i = 0;

while (1) {
        print ">> ",$i++,"\n";
        sleep 1;

Quote:
}

Any help would be appreciated.

--

Alastair




Sat, 30 Jun 2001 03:00:00 GMT  
 Getting program to restart (HUP) more than once

Quote:

> I would be grateful if someone could help with this. I am writing a
> program that installs a signal handler or two. One signal I want to
> catch is a 'hang-up' (HUP) signal. The program does catch the HUP and
> restart correctly, but will only do this once. It then ignores any
> subsequent HUP.

perldoc perlipc, and search for the word 'loathe' .  Non-BSD systems
generally make you reset the signal handler each time it gets
triggered.

Be careful here -- on systems that *do* leave the handler in place (I
know for sure FreeBSD has problems with this), repeatedly calling and
resetting a signal handler in Perl can bring on massively ugly memory
leaks.
--
"Meow."  --me



Sat, 30 Jun 2001 03:00:00 GMT  
 Getting program to restart (HUP) more than once

Quote:

>perldoc perlipc, and search for the word 'loathe' .  Non-BSD systems
>generally make you reset the signal handler each time it gets
>triggered.

Thanks for the reply.

I have actually looked at as much as I could find on this subject, including
perlipc, the cookbook and the camel. I thought I was doing a handler reset
inside the handler itself (I see the 'loathe' comments and considered myself
'paranoid' - see pages 588-589 of the cookbook).

I've obviously a lot of learn here.

Is my signal handler blocked with the exec?

The following code works i.e. resets at each HUP (but does not 'restart' the
actual program itself) ;

#!/usr/bin/perl -w

my $hup = 0;

sub hup_handler {
        $hup = 1;

Quote:
}

$SIG{HUP} = \&hup_handler;

my $i = 0;

while (1) {
        if ($hup) {
                print "Restart ...\n";
                $hup = 0;
                $i = 0;
        }
        print ">> ",$i++,"\n";
        sleep 1;

Quote:
}

I'd rather re-exec the whole program (and be able to consistently).

Thanks.

--

Alastair




Sat, 30 Jun 2001 03:00:00 GMT  
 Getting program to restart (HUP) more than once

Quote:

> Is my signal handler blocked with the exec?

Yup, I think it was, sort of.  Perl leaves the signal trapped in never-never
land if you exec inside a handler.  Resetting the signal to default or
ignore beforehand, using something like exit(system $0) in there to get a
new process instead of exec'ing, all leave you in the same boat.  Bummer,
eh?

The following version works.  Notice that I have to test *outside* the
handler to see if the signal has been tripped (that gets flagged in the
$HUPPED variable), and then call my exec.  This is probably for the
best anyway, because you get the opportunity to clean up your process
gracefully.  Things that care about signals generally have loops, so this
works out pretty well even if it's less than beautiful.

#!/usr/bin/perl -w
use strict;

my $HUPPED = 0;

$SIG{HUP} = \&huphandler;

my $foo = 0;
print "Beginning...\n";
for (;;) {
   print "[$$] meow ", $foo++, "\n";
   sleep 1;
   if ($HUPPED) {
     print "Yeowch!\n";
     exec $0;
   }

Quote:
}

sub huphandler {
  $HUPPED = 1;

Quote:
}

#eof


Sat, 30 Jun 2001 03:00:00 GMT  
 Getting program to restart (HUP) more than once

Quote:

>The following version works.  Notice that I have to test *outside* the
>handler to see if the signal has been tripped (that gets flagged in the
>$HUPPED variable), and then call my exec.

Thanks for the response, once again. Your program certainly works and has helped
to re-formulate my effort.

Much appreciated!

--

Alastair




Sat, 30 Jun 2001 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. kill -HUP only works once

2. HUP signals, more than once?

3. program monitoring and restart

4. Restart a perl program (Without re-running?)

5. Redirecting STDERR more than once in a program.

6. programs only run to completion once

7. mod_perl: first program executes only once

8. C-Shell HUP Signal on exit

9. kill -HUP $pid problem

10. HUP signal handling

11. catching HUP signal

12. HUP while waiting on named pipe read causes interrupted system call

 

 
Powered by phpBB® Forum Software