I have written a small pygtk app that stays resident as a status icon. When the application exits it must clean up some things. It works well when I exit the app, send SIGTERM, send Ctrl-C, but if I logout from Gnome having the app open, it gets killed without clean up. I suspect a SIGKILL being sent to the application, but shouldn't it be a SIGTERM??
Here is an example of what I try to explain. A log file is created in ~/test.log. The correct behaviour on shutdown is to get through the "# do cleanup here" section (ie. writing "Exiting application" to the log file).
Code:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pygtk
pygtk.require('2.0')
import gtk
import os
import sys
import time
import signal
def write_log(message):
f = open(os.path.expanduser("~") + "/test.log", 'a')
f.write(message+"\n")
f.close()
# SIGTERM handler
def exittermhandler(signum, frame):
print "SIGTERM detected!"
write_log("SIGTERM detected!")
sys.exit(1)
signal.signal(signal.SIGTERM, exittermhandler)
class TestApp:
def __init__(self):
print "Opening application"
write_log("Opening application")
def destroy(self, widget, data=None):
print "Destroy signal occurred"
write_log("Destroy signal occurred")
gtk.main_quit()
def main(self):
self.icon = gtk.status_icon_new_from_stock(gtk.STOCK_HOME)
self.icon.set_tooltip("Test Application")
self.icon.connect('activate', self.destroy)
try:
gtk.main()
except (KeyboardInterrupt):
print "Terminated by user"
write_log("Terminated by user")
except (SystemExit):
print "System Exit"
write_log("System Exit")
except:
raise
finally:
print "Exiting application"
write_log("Exiting application")
# do cleanup here
# ...
if __name__ == "__main__":
test_app = TestApp()
test_app.main()
Anybody has a hint ?? This gives me headaches. Thank you in advance.
Bookmarks