properly catch SIGTERM

Eric Frederich eric.frederich at gmail.com
Thu Jul 19 15:51:33 EDT 2012


So I wrote a script which acts like a daemon.
And it starts with something like this....

########### Begin Code

import signal

STOPIT = False

def my_SIGTERM_handler(signum, frame):
    global STOPIT
    print '\n--- Caught SIGTERM; Attempting to quit gracefully ---'
    STOPIT = True

signal.signal(signal.SIGTERM, my_SIGTERM_handler)
signal.signal(signal.SIGINT , my_SIGTERM_handler)

########### End Code

My main loop looks something like this...

login()
while not STOPIT:
    foo1()
    foo2()
    foo3()
    if STOPIT:
        break
    bar1()
    bar2()
    bar3()

print 'bye'
logout()

This seems to work okay but just now I got this while hitting ctrl-c
It seems to have caught the signal at or in the middle of a call to
sys.stdout.flush()


--- Caught SIGTERM; Attempting to quit gracefully ---
Traceback (most recent call last):
  File "/home/user/test.py", line 125, in <module>
    sys.stdout.flush()
IOError: [Errno 4] Interrupted system call


How should I fix this?
Am I doing this completely wrong?

Thanks,
~Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120719/e0cd7016/attachment.html>


More information about the Python-list mailing list