Universal way to logout from terminal via dbus

I’m new with dbus, and saw different ways to log out from terminal depending on desktop env. But I’m curious is there any way to log out from any desktop env using dbus messages?

On GNOME:

dbus-send --session --type=method_call --print-reply --dest=org.gnome.SessionManager /org/gnome/SessionManager org.gnome.SessionManager.Logout uint32:1

On KDE:

dbus-send --print-reply --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.logout int32:0 int32:0 int32:0

Is there any command that would work on every desktop env (like using system dbus)?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

On systemd setups you should be able to forcibly terminate a session via logind dbus interface:

dbus-send --system --print-reply --dest=org.freedesktop.login1 
/org/freedesktop/login1 'org.freedesktop.login1.Manager.TerminateSession' 
string:c2

where c2 is the session ID string; to get the session ID you could run

loginctl
    SESSION       UID  USER           SEAT            
        c1        120  gdm            seat0           
        c2       1000  don            seat0           

2 sessions listed

or, if you prefer to do it also via dbus interface:

dbus-send --system --print-reply --dest=org.freedesktop.login1 
/org/freedesktop/login1 'org.freedesktop.login1.Manager.ListSessions'

which returns something like

   array [
      struct {
         string "c1"
         uint32 120
         string "gdm"
         string "seat0"
         object path "/org/freedesktop/login1/session/c1"
      }
      struct {
         string "c2"
         uint32 1000
         string "don"
         string "seat0"
         object path "/org/freedesktop/login1/session/c2"
      }
   ]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x