在Linux下使用Python/GTK检测用户注销/关机 - SIGTERM/HUP未接收到
好的,这个问题看起来有点棘手。我有一个使用pyGTK开发的应用程序,它会因为X窗口错误而随机崩溃,我无法捕捉或控制这些错误。
所以我创建了一个包装程序,一旦检测到崩溃就会重启这个应用程序。现在的问题是,当用户注销或关闭系统时,应用程序会以状态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
正常情况下,这应该在注销时创建term文件,在以&符号启动时创建hup文件。但在我的系统上并没有这样做。GDM根本不在乎这个脚本,当我重新登录时,它仍然在运行。
我还尝试过使用shopt -s huponexit
,但也没有成功。
编辑2:
这里还有一些关于实际代码的更多信息,整个流程看起来是这样的:
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。这意味着要么是它自己挂起了,要么是父进程终止了,主要的问题是弄清楚这两者中到底发生了哪一个。X错误也会导致返回1。在shell脚本中捕捉这些错误并不奏效。
如果你想看看实际的代码,可以在GitHub上查看:
http://github.com/BonsaiDen/Atarashii
2 个回答
你忘记关闭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)
其余的代码和上面的一模一样,没有任何变化。当我向python进程发送 SIGTERM
时,它在这里可以正常工作:gtk主循环结束,程序以退出代码 0
退出。
好的,我终于找到了解决办法 :)
在这种情况下,你不能依赖信号。你必须连接到桌面会话,才能收到注销即将发生的通知。
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(一个桌面环境)不会识别你的程序已经退出,会弹出对话框告诉你还有一些程序在运行。