Detect user logout / shutdown in Python / GTK under Linux

Posted by Ivo Wetzel on Stack Overflow See other posts from Stack Overflow or by Ivo Wetzel
Published on 2010-03-22T05:32:59Z Indexed on 2010/03/22 5:41 UTC
Read the original article Hit count: 1637

Filed under:
|
|
|

OK this is presumably a hard one, I've got an pyGTK application that has random crashes due to X Window errors that I can't catch/control.

So I created a wrapper that restarts the app as soon as it detects a crash, now comes the problem, when the user logs out or shuts down the system, the app exits with status 1. But on some X errors it does so too.

So I tried literally anything to catch the shutdown/logout, with no success, here's what I've tried:

import pygtk
import gtk
import sys


class Test(gtk.Window):
    def delete_event(self, widget, event, data=None):
        open("delete_event", "wb")

    def destroy_event(self, widget, data=None):
        open("destroy_event", "wb")

    def destroy_event2(self, widget, event, data=None):
        open("destroy_event2", "wb")

    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.show()
        self.connect("delete_event", self.delete_event)
        self.connect("destroy", self.destroy_event)
        self.connect("destroy-event", self.destroy_event2)      

def foo():
    open("add_event", "wb")

def ex():
    open("sys_event", "wb")


from signal import *
def clean(sig):
    f = open("sig_event", "wb")
    f.write(str(sig))
    f.close()
    exit(0)

for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
    signal(sig, lambda *args: clean(sig))


def at():
    open("at_event", "wb")

import atexit
atexit.register(at)

f = Test()
sys.exitfunc = ex
gtk.quit_add(gtk.main_level(), foo)

gtk.main()
open("exit_event", "wb")

Not one of these succeeds, is there any low level way to detect the system shutdown? Google didn't find anything related to that.

I guess there must be a way, am I right? :/

© Stack Overflow or respective owner

Related posts about python

Related posts about linux