OK, this seems to be complicated, I have a pyGTK application that has random crashes due to X Window errors that I cannot catch / control.
So, I created a shell that restarts the application as soon as it detects a failure, now there is a problem when the user logs out or shuts down the system, the application exits with status 1. But on some errors X does this too.
So, I literally literally tried to catch a shutdown / logout, without success, this is what I 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")
None of these options work, is there a low-level way to detect system shutdown? Google did not find anything related to this.
I think there must be a way, am I right?: /
EDIT: Alright, more stuff.
I 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
Usually this should create a term file at logout and a hup file when it was launched with &. But not in my System. GDM doesn't care about the script at all, when I reboot, it still works.
I also tried using shopt -s huponexit , without any success.
EDIT2:
Also here is additional information about the real code, it all looks like this:
Wrapper Script, that catches errors and restarts the programm -> Main Programm with GTK Mainloop -> Background Updater Thread
The stream is as follows:
Start Wrapper -> enter restart loop while restarts < max: -> start program -> check return code -> write error to file or exit the wrapper on 0
Now, at the end of work, start program return 1. This means that it is either hanup or the parent process is complete, the main problem is to find out which of the two has occurred. X Errors also lead to 1. The trap in shellscript does not work.
If you want to take a look at the actual code, check it out on GitHub:
http://github.com/BonsaiDen/Atarashii