How to stop (inhibit) Gnome from shutting down with Python

Posted
import dbus

bus = dbus.SessionBus()
bus.call_blocking(
    bus_name="org.gnome.SessionManager",
    object_path="/org/gnome/SessionManager",
    dbus_interface="org.gnome.SessionManager",
    method="Inhibit",
    signature="susu",
    args=["your_app_id", 0, "I'm currently doing something...", 1 | 2 | 4 | 8])

This can stop Gnome from automatically shutting down (or suspending, logging out, etc.). However you will still be able to manually shut it down, Gnome may present a warning1 with some of the values in args.

You can discover the correct dbus_interface, method and signature by using:

$ busctl --user introspect org.gnome.SessionManager /org/gnome/SessionManager
org.gnome.SessionManager            interface -         -            -   # <- dbus_interface
.CanRebootToFirmwareSetup           method    -         b            -
.CanShutdown                        method    -         b            -
.GetClients                         method    -         ao           -
.GetInhibitors                      method    -         ao           -
.GetLocale                          method    i         s            -
.Inhibit                            method    susu      u            -   # <- method and signature
...

The signature susu means it expects four arguments: a string, a uint32, a string and a uint32.

For what the actual args are though, I couldn’t find a better documentation than the source code that defines them.


  1. From what I’ve seen your_app_id must be valid for Gnome to present the warning. ↩︎