Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Handle SIGHUP with AnyEvent

by nutris (Initiate)
on Aug 23, 2011 at 09:32 UTC ( [id://921850]=perlquestion: print w/replies, xml ) Need Help??

nutris has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I am trying to use AnyEvent to handle signals (SIGHUP, SIGTERM, etc.). I think a I have to use AnyEvent since I need the process to listen to signals and filesystem events (via AnyEvent::Filesys::Notify).

According to the documentation at http://search.cpan.org/~mlehmann/AnyEvent-5.34/lib/AnyEvent.pm#SIGNAL_WATCHERS I am trying the following:

#!/usr/bin/perl use strict; use warnings; use AnyEvent; # set up event handler my $w = AnyEvent->signal (signal => "HUP", cb => sub { print "Got SIGH +UP, reloading..."; }); # wait for event $done->recv; exit 0;

Running this code on Debian Squeeze (Perl 5.10) and sending a SIGHUP to the process via kill -HUP 12345 perl just quits with the message "Hangup" spooled to STDIN.

What am I doing wrong? I would expect to see my message "Got SIGHUP, reloading..." before the exit. But the message "Hangup" makes me feel like the signal was not handled by AnyEvent but directly by perl.

Any suggestions?

Thanks.

nutris

Replies are listed 'Best First'.
Re: Handle SIGHUP with AnyEvent
by moritz (Cardinal) on Aug 23, 2011 at 09:39 UTC
      Yeah, you are - of course - right. But the problem persists with the corrected code.
Re: Handle SIGHUP with AnyEvent
by Anonymous Monk on Aug 23, 2011 at 09:40 UTC
    Please post your real code.
    String found where operator expected at 921850.pl line 9, near "echo " +Got SIGHUP, reloading..."" (Do you need to predeclare echo?) syntax error at 921850.pl line 9, near "echo "Got SIGHUP, reloading... +"" Global symbol "$done" requires explicit package name at 921850.pl line + 12. Execution of 921850.pl aborted due to compilation errors.
Re: Handle SIGHUP with AnyEvent
by Neighbour (Friar) on Aug 24, 2011 at 06:50 UTC
    #!/usr/bin/perl use strict; use warnings; use AnyEvent; $|++; my $done = AnyEvent->condvar; # set up event handler my $w = AnyEvent->signal(signal => "HUP", cb => sub { print "Got SIGHU +P, reloading...\n"; }); # wait for event $done->recv; exit 0;

    Works fine. Don't forget that stdout is buffered (which $|++ fixes.
      Doesn't work for me. Running your script as is, it exits with "Terminated: 15" when sending it a SIGHUP. I came across this thread when I couldn't catch SIGALRM using AnyEvent with the EV loop, and had to resort to using a plain old perl signal handler.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://921850]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 15:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found