Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Welcome to the Monastery
 
PerlMonks  

Re: Controlling a Perl Daemon with Signals

by sk (Curate)
on Aug 05, 2005 at 17:46 UTC ( [id://481376]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Controlling a Perl Daemon with Signals

You are saying the script is not terminating right after the interrupt is issued?

I took your code and added a sub that i can test and also changed your TERM to INT so it is easy for me to test

#!/usr/bin/perl -w use constant MAX_ITERATIONS => 1000000; use constant MAX_TIME => 600; use warnings; use strict; my $start_time = time; my $iterations = 0; my $exit_requested = 0; my $handling_request = 0; sub sig_handler { $exit_requested = 1; print "Caught interrupt\n"; # exit(0) if !$handling_request; } sub handle_request { print ("hi: $iterations\n") if $iterations % 10000 == 0; } $SIG{INT} = \&sig_handler; # init(); while (!$exit_requested && $iterations <= MAX_ITERATIONS && (time - $s +tart_time) < MAX_TIME) { $handling_request = 1; handle_request(); $iterations++; $handling_request = 0; } print ("Exit value: Iteration = $iterations exit_req = $exit_requested +\n");

Output Here when I run the script and hit ^C while it is running I get this output

hi: 0 hi: 10000 hi: 20000 hi: 30000 hi: 40000 hi: 50000 hi: 60000 hi: 70000 hi: 80000 hi: 90000 hi: 100000 hi: 110000 hi: 120000 hi: 130000 Caught interrupt Exit value: Iteration = 133660 exit_req = 1
Seems to suggest that it picked the interrupt and terminated the loop at that point.

Is this the behavior you want and you don't see that? Are you sure you are sending TERM signals?

-SK

Replies are listed 'Best First'.
Re^2: Controlling a Perl Daemon with Signals
by eastcoastcoder (Sexton) on Aug 05, 2005 at 18:30 UTC
    Right - I'm not getting that behaviour.

    I am sure I'm sending term (kill -TERM <pid>)

    Another fact: When I do kill -TERM, instead of exiting, the program seems to freeze, except that memory usage climbs indefintely - so it must be in some time of endless loop!

      I modified the code to handle TERM signal and sent TERM from command prompt. It caught correctly and gave me expected results.

      I am guessing this not a web-based prog, if so I am not well-versed to explain why you it is not working for you.

      Regarding the jch341277's reply on TERM signal, I am not sure whether it is applicable to your case. For example, If the program was entering the condition in the  while block, it should execute just one more time and quit as you do not modify  $exit_requested inside the while block. Other cases should terminate next time the while condition is evaluated

      Can you try the sample program which prints "hi" and see if the same problem occurs. If it does it is very strange as it works just fine for me and I don't see any mistakes in your code.

      However if the above sample code works but not your original code then you might have to check your  handle_request() sub.

      -SK

        sk - I agree, it depends on what handle_request actually does. I just figured that it doesn't deal with the interruption gracefully; mainly because on my first attempt to run the code my handle_request looked like this:

        sub handle_request { sleep 13; return 1; }
        Which looked like it was doing what the OP is having trouble with.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://481376]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.