Detect user logout / shutdown in Python / GTK under Linux - SIGTERM/HUP not received
OK, I finally found the solution :)
You simply can't rely on signals in this case. You have to connect to the Desktop Session in order to get notified that a logout is going to happen.
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
One important note here: Don't try to exit your program in on_logout
, if you do so, GNOME won't recognize that your program has been exited and will give you the dialog that some programs are still running.
How can I catch a system suspend event in Python?
i think simplest method would be to use DBUS python interface
and listen for 'AboutToSleep' and/or 'Sleeping' event on 'org.freedesktop.UPower' interface
Python + Windows: How to write a script that pops up a reminder on computer sleep/shutdown?
This will tell you how to detect event
Linux:- Detect user logout / shutdown in Python / GTK under Linux - SIGTERM/HUP not received.
Windows :- Python - Windows Shutdown Events
And this will pop up notice
from tkinter import *
root = Tk()
w = Label(root, text="Take your Medicine \n :):)...Get Well Soon...")
w.pack()
root.mainloop()
How can I catch a system suspend event in Python?
i think simplest method would be to use DBUS python interface
and listen for 'AboutToSleep' and/or 'Sleeping' event on 'org.freedesktop.UPower' interface
Related Topics
"E: Unable to Locate Package Python-Pip" on Ubuntu 18.04
How to Obtain Ports That a Process in Listening On
Pip Install Unable to Find Ffi.H Even Though It Recognizes Libffi
How to List All Python Virtual Environments in Linux
Automatic Detection of Display Availability with Matplotlib
How to Convert .Pcm Files to .Wav Files (Scripting)
How to Properly Write to Fifos in Python
Asyncio in Corroutine Runtimeerror: No Running Event Loop
Get File Creation Time with Python on Linux
Cannot Get Environment Variables in Django Settings File
Process Dies, If It Is Run via Paramiko Ssh Session and with "&" in the End
Is This the Right Way to Run a Shell Script Inside Python
Fastest Way to Get System Uptime in Python in Linux
Can't Build Matplotlib (Png Package Issue)