好吧,这可能是一个困难的问题,我有一个 pyGTK 应用程序,由于我无法捕获/控制的 X Window 错误而随机崩溃。
因此,我创建了一个包装器,一旦检测到崩溃,它就会重新启动应用程序,现在问题来了,当用户注销或关闭系统时,应用程序会以状态 1 退出。但在某些 X 错误上,它也会这样做。
所以我尝试了任何方法来捕获关闭/注销,但没有成功,这就是我尝试过的:
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")
这些都没有成功,是否有任何低级方法来检测系统关闭?谷歌没有找到任何与此相关的内容。
我想一定有办法,对吗? :/
EDIT:好吧,还有更多东西。
我创建了这个 shell 脚本:
#!/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
还创建了一个 .desktop 文件来运行它:
[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
通常,这应该在注销时创建术语文件,并在以 & 启动时创建 hup 文件。但不在我的系统上。 GDM 根本不关心脚本,当我重新登录时,它仍然在运行。
我也尝试过使用shopt -s huponexit
,没有成功。
EDIT2:
这里还有一些关于真实代码的更多信息,整个事情看起来像这样:
Wrapper Script, that catches errors and restarts the programm
-> Main Programm with GTK Mainloop
-> Background Updater Thread
流程是这样的:
Start Wrapper
-> enter restart loop
while restarts < max:
-> start program
-> check return code
-> write error to file or exit the wrapper on 0
现在处于关机状态,start program
return 1。这意味着要么它完成了 hanup,要么父进程终止了,主要问题是找出这两者中的哪一个刚刚发生。
X 错误也会导致 1。捕获 shellscript 不起作用。
如果您想查看实际代码,请访问 GitHub:
http://github.com/BonsaiDen/Atarashii