8

好的,这可能是一个困难的问题,我有一个 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")

这些都没有成功,是否有任何低级方法来检测系统关闭?谷歌没有找到任何与此相关的东西。

我想一定有办法,对吗?:/

编辑: 好的,更多的东西。

我创建了这个 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返回 1。这意味着它确实 hanup 或父进程终止,主要问题是找出这两个中的哪一个刚刚发生。X 错误也会导致 1。困在 shellscript 中是行不通的。

如果您想查看实际代码,请查看 GitHub:http:
//github.com/BonsaiDen/Atarashii

4

2 回答 2

2

好的,我终于找到了解决方案:)

在这种情况下,您根本不能依赖信号。您必须连接到桌面会话才能收到将要注销的通知。

import gnome.ui

gnome.program_init('Program', self.version) # This is going to trigger a warning that program name has been set twice, you can ignore this, it seems to be a problem with a recent version of glib, the warning is all over the place out there
client = gnome.ui.master_client() # connect us to gnome session manager, we need to init the program before this
client.connect('save-yourself', self.on_logout) # This gets called when the user confirms the logout/shutdown
client.connect('shutdown-cancelled', self.on_logout_cancel) # This gets called when the logout/shutdown is canceled
client.connect('die', self.on_logout) # Don't know when this gets called it never got in my tests

def on_logout(self, *args):
    # save settings an create a file that tells the wrapper that we have exited correctly!
    # we'll still return with status code 1, but that's just gtk crashing somehow

def on_logout_cancel(self, *args):
    # simply delete the logout file if it exists

这里有一个重要的注意事项:不要试图退出你的程序on_logout,如果你这样做了,GNOME 将不会识别你的程序已经退出并且会给你一些程序仍在运行的对话框。

于 2010-03-25T22:18:50.567 回答
0

你忘了关闭 gtk 的事件循环。

0当您关闭窗口时,此代码与代码一起退出:

import gtk

class Test(gtk.Window):
    def destroy_event(self, widget, data=None):
        gtk.main_quit()

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

f = Test()
gtk.main()

编辑:这是捕获 SIGTERM 信号的代码:

import signal

def handler(signum, frame):
    print 'Signal handler called with signal', signum
    print 'Finalizing main loop'
    gtk.main_quit()

signal.signal(signal.SIGTERM, handler)

其余代码与上面完全相同,没有任何变化。当我发送SIGTERM到 python 进程时,它在这里工作: gtk 主循环结束并且程序以退出代码退出0

于 2010-03-22T11:50:29.300 回答