Detect user logout / shutdown in Python / GTK under Linux - SIGTERM/HUP not received
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/23
1:51 UTC
Read the original article
Hit count: 781
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? :/
EDIT: OK, more stuff.
I've created this shell script:
#!/bin/bash
trap test_term TERM
trap test_hup HUP
test_term(){
echo "teeeeeeeeeerm" >~/Desktop/term.info
exit 0
}
test_hup(){
echo "huuuuuuuuuuup" >~/Desktop/hup.info
exit 1
}
while [ true ]
do
echo "idle..."
sleep 2
done
And also created a .desktop file to run it:
[Desktop Entry]
Name=Kittens
GenericName=Kittens
Comment=Kitten Script
Exec=kittens
StartupNotify=true
Terminal=false
Encoding=UTF-8
Type=Application
Categories=Network;GTK;
Name[de_DE]=Kittens
Normally this should create the term file on logout and the hup file when it has been started with &. But not on my System. GDM doesn't care about the script at all, when I relog, it's still running.
I've also tried using shopt -s huponexit
, with no success.
EDIT2:
Also here's some more information aboute the real code, the whole thing looks like this:
Wrapper Script, that catches errors and restarts the programm
-> Main Programm with GTK Mainloop
-> Background Updater Thread
The flow is like this:
Start Wrapper
-> enter restart loop
while restarts < max:
-> start program
-> check return code
-> write error to file or exit the wrapper on 0
Now on shutdown, start program
return 1. That means either it did hanup or the parent process terminated, the main problem is to figure out which of these two did just happen.
X Errors result in a 1 too. Trapping in the shellscript doesn't work.
If you want to take a look at the actual code check it out over at GitHub:
http://github.com/BonsaiDen/Atarashii
© Stack Overflow or respective owner