在Linux下的Python/GTK中检测用户注销/关机-未收到SIGTERM/HUP
好吧,这可能是一个困难的问题,我有一个pyGTK应用程序,它由于X窗口错误而随机崩溃,我无法捕获/控制 因此,我创建了一个包装器,在检测到崩溃时立即重新启动应用程序,现在问题来了,当用户注销或关闭系统时,应用程序将以状态1退出。但在某些X错误上,它也会这样做 因此,我尝试了任何方法来捕获关闭/注销,但没有成功,以下是我尝试的:在Linux下的Python/GTK中检测用户注销/关机-未收到SIGTERM/HUP,python,linux,gtk,pygtk,sigterm,Python,Linux,Gtk,Pygtk,Sigterm,好吧,这可能是一个困难的问题,我有一个pyGTK应用程序,它由于X窗口错误而随机崩溃,我无法捕获/控制 因此,我创建了一个包装器,在检测到崩溃时立即重新启动应用程序,现在问题来了,当用户注销或关闭系统时,应用程序将以状态1退出。但在某些X错误上,它也会这样做 因此,我尝试了任何方法来捕获关闭/注销,但没有成功,以下是我尝试的: import pygtk import gtk import sys class Test(gtk.Window): def delete_event(sel
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-shuponexit,但没有成功
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
流程如下:
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
启动包装器
->进入重启循环
重新启动时<最大值:
->启动程序
->检查返回代码
->将错误写入文件或退出0上的包装
现在关机时,启动程序
返回1。这意味着要么是它做了hanup,要么是父进程终止,主要问题是要弄清楚这两个进程中哪一个刚刚发生了。
X错误也会导致1。在shellscript中进行捕获不起作用
如果您想查看实际代码,请在GitHub上查看:您忘记关闭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
好,我终于找到了解决方案:)
在这种情况下,你不能仅仅依靠信号。您必须连接到桌面会话,才能收到即将注销的通知
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
这里有一个重要的提示:不要试图在退出时退出
,如果这样做,GNOME将无法识别您的程序已退出,并会给您一个对话框,说明某些程序仍在运行。这并不能解决问题,当用户注销或关闭系统时,甚至不会调用destroy\u事件,上述文件均未创建。另一个问题是由于包装器脚本,它首先被“杀死”,因此应用程序将以1退出,因为其父进程已离开。进一步的研究表明,实际上该进程应该得到一个SIGTERM。但是它没有得到一个,似乎它只是被杀死了…根据GDM的来源,它首先发送SIGTERM,似乎python只是不在乎:/仍然无法工作,似乎GnomeDesktopManager不知何故被吓坏了,我连接到了每个信号,但没有收到一个,我想它真的只是做了一个杀死。我还创建了一个包含所有信号的shell脚本,但仍然没有成功。@Ivo Wetzel:我无法重现这个问题。在这里,它的工作完美无瑕。我已经让它在SIGTERM上创建了一个文件,当我注销时,它创建的文件很好。在Ubuntu 9.10上使用GDM。这一定是你在代码中做的其他事情(你在使用线程吗?)