How can I block or delay system shutdown in Linux using a Qt (PySide6) application?
-
Hi everyone,
I'm developing a cross-platform desktop application using PySide6 (Qt for Python) on Linux (Ubuntu 22.04, GNOME). One of the key features of the application involves running critical tasks (e.g., backups), during which I want to temporarily block or delay system shutdown — especially if initiated via the GUI (like GNOME’s power-off menu).
What I’ve tried so far:
-
Using
systemd-inhibit
- It works for terminal-triggered shutdowns (
systemctl poweroff
), but GUI shutdown dialogs ignore it.
- It works for terminal-triggered shutdowns (
-
D-Bus (logind/org.freedesktop.login1)
- Acquired inhibitor locks via D-Bus using
Inhibit()
onorg.freedesktop.login1.Manager
. - Successfully prevents terminal shutdowns, but again, GUI shutdown is unaffected.
- Acquired inhibitor locks via D-Bus using
-
GNOME Session Manager
- Explored using
org.gnome.SessionManager
’sInhibit()
method. - It works only on GNOME, not across other DEs like KDE or XFCE.
- Explored using
The requirement:
- Must be non-root, i.e., work in user space.
- Must not require modifying system-level files, polkit rules, or shutdown scripts.
- Should work across multiple desktop environments if possible.
- Goal is to inform or prevent shutdown temporarily during a critical operation, then release the block once done.
Questions:
- Is there a Qt-native or Qt-compatible way to block or delay system shutdown while a long-running task is active?
- Are there any Qt APIs or DBus wrappers you recommend for this use case?
-
-
@Ghost-Uchiha said in How can I block or delay system shutdown in Linux using a Qt (PySide6) application?:
Is there a Qt-native or Qt-compatible way to block or delay system shutdown while a long-running task is active?
No, nothing to do with Qt.
If you can even do this, and you require non-root, plus no system file alterations, it would not surprise me if you needed to communicate a request from your user app to, say, a root privilege app.
-
@JonB
Thank you for the clarification — that makes sense. I agree this is more of a system/desktop-level concern than something Qt directly handles.I’m trying to avoid root privileges or installing any privileged helpers since the app is intended to be open source and portable across desktop environments.
So far I’ve had partial success using:
systemd-inhibit
- D-Bus with
org.freedesktop.login1
- GNOME Session Manager’s
Inhibit()
method
But none of these seem to reliably prevent GUI shutdowns across desktops. If there's a way to register my app with the session manager or logind in a way that the DE respects (without requiring Polkit modification), I'd love to hear about it.
Appreciate the insight!
-
Hi and welcome to devnet,
I think that you are looking for QGuiApplication::commitDataRequest.
-
@SGaist
Thanks for the suggestion! I’ll definitely try usingcommitDataRequest()
.Just to clarify — will this method reliably block or cancel GUI-initiated shutdowns (like using the power-off button from GNOME or KDE) if I call
manager.cancel()
inside that slot?Also, does this approach still work reliably with modern session managers, given that some DEs (like GNOME) are moving away from traditional X11 session management?
Are there any best practices for using
commitDataRequest()
together with D-Bus inhibitors ororg.freedesktop.login1
to improve compatibility across desktop environments?Thanks again — I appreciate your insights!
4/5