Reputation: 188
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?
Upvotes: 0
Views: 1290
Reputation: 3405
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.
Upvotes: 1