Say you have a DBus service on the session bus (the per-user bus). Your service might get started via an /etc/xdg/autostart/* file, or it might get started the first time some app tries to use your well-known service name. Either way, let's say that the DBus session bus is the process that starts your service.

And then the user logs out. So what happens? Does DBus send any actionable SIG<foo> signals to your service, or is it strait to SIGKILL? Is there any other way to know the user is logging out? Basically, I have some cleanup actions I need to run, which includes killing sub-processes started with subprocess.Popen and multiprocessing.

Now you before you say, "Popen and multiprocessing are evil, don't use them", in my current experiment, I'm not using them. I'm just trying to figure out how a Python DBus service can hook into something that allows it to run cleanup actions, whatever the overall architecture might be.

Any advice? Any examples? Note that my example will be running in a GObject.MainLoop and is using Python3 on Ubuntu Precise (or Quantal).


You can either do something like this:

bus = dbus.SessionBus()

Or you can connect to the NameLost signal on the org.FreeDesktop.DBus interface. The former doesn't let you pass additional arguments, and your method can only take the bus connection object itself as an argument. The latter is a bit more complicated, but doesn't seem to allow passing in other arguments of your own accord either, and you are limited to taking the arguments which the signal itself sends, which in this case is a string of the message bus name your process formerly owned.

On the other hand, if your process does stay around after that, you should theoretically get a SIGKILL at some point as well.


If you only care about running under GNOME & Unity you should be able to hook into the session manager DBus interface. That not only gives you a “about to log out” message via the QueryEndSession and EndSession signals, but also allows you to block logout/shutdown until you're done cleaning up if you want, via EndSessionResponse.

I'm not sure to what extent KDE implements a compatible interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.