0

I am sending SIGTERM signal to my server via start-stop-daemon. As expected the server shuts down when signal is received. Now I want to halt my server for 10 sec after receiving SIGTERM and then shut it down. To achieve this I wrote

def sigterm_handler(signum):
    time.sleep(10)
    sys.exit(0)    

Class server(object):
    .
    .

    def __call__()
        signal.signal(signal.SIGTERM, sigterm_handler)
        #some code

    .
    .

Now the server never waits for 10 secs after receiving signal. As soon as the signal is send, it shuts down (checked in loggers).

Also the following message halts for 10 secs on my terminal.

* Stopping Server:

I think my code is not able to tell python that the signal is being handled and so, the default handler is also being called along with my handler. Any idea what needs to be done?

1 Answer 1

1

The problem is your sigterm_handler. It blocks your server as soon as it recieves the SIGTERM for 10s and then shut it down.

What you really want is "ignoring" the signal and then exit the server after 10s (for example with another signal):

shutdown_mode=False

def alarm_handler(signum, frame):
        sys.exit(0)

def sigterm_handler(signum):
     global shutdown_mode
     if not shutdown_mode:
            shutdown_mode=True
            signal.signal(signal.SIGALRM, alarm_handler)
            signal.alarm(10)

You want also switch into a "shutdown mode", where e.g. the server should not accept new connections etc.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks it worked. Regarding the "shutdown mode", the code seems to stop working as soon as I introduced it (corrected the "shutdownMode" ,"shutdown_mode" typo). Even making a log of shutdown_mode at the beginning of sigterm_handler stopped the functionality.
@akipro Thanks. "shutdown_mode" did not work as its global declaration was missing. It should work now.
ahhh... should have noticed that. Thanks again.
hey, it seems that doing any other operation in sigterm_handler other than setting alarm, like setting shutdown_mode or any other variable, the server again stops immediately.
For debugging, run the server inside a shell. Probably, you get an python error. For me, e.g. setting variables work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.