[Tip] Logout of Plasma Session from the Terminal

Since this is a terminal-centric distro, you might want to logout from your Plasma session using the terminal. I just discovered this so I wanted to share it.

This command does that:

qdbus org.kde.ksmserver /KSMServer logout 0 0 1

If you replace 0 0 0 in that command with 1 0 1, it will display the logout window giving you the option to cancel it, shutdown or restart your computer. This does exactly the same thing as clicking on the Log out button, saves your session and everything.

Of course, that is a lot of typing, so we can set an alias.

If you’re using bash, add this to your .bashrc:

alias logout="shopt -q login_shell && logout || qdbus org.kde.ksmserver /KSMServer logout 0 0 1"

Or if you’re using zsh, add this to your .zshrc:

alias logout="[[ -o login ]] && logout || qdbus org.kde.ksmserver /KSMServer logout 0 0 1"

Then you can logout by running:

logout

The alias overrides the normal logout command which you’d use in the TTY to log out of a login session. To retain the original functionality, it first tests whether you’re in a login shell (in which case it runs the normal logout command) or, if you’re not in a login shell, it runs that long qdbus command that logs you out of your Plasma session.

I have not tested the bash version, but the zsh version works like a charm. :ok_hand:

7 Likes

By changing the numbers in the command you can do other log-out-related stuff.

As mentioned above

qdbus org.kde.ksmserver /KSMServer logout 1 0 1

will display the logout menu, with the “Log Out” button selected.

qdbus org.kde.ksmserver /KSMServer logout 1 1 1

will do the same, but with the “Restart” button selected. Changing it to 0 1 1 will restart your computer without prompting you. 0 2 1 will shut it down.

I’ll leave it to you to figure out what the other combinations do :wink:

At the moment, I have no idea what the third number does.

EDIT: I just did some more duckduckgoing and I found out an old post on Stack Overflow about it: https://askubuntu.com/questions/1871/how-can-i-safely-shutdown-reboot-logout-kde-from-the-command-line#1876

This may be outdated, but it appears that the three numbers are the following:

ShutdownConfirm ShutdownType ShutdownMode

ShutdownConfirm can take the following values:

-1: default
0: no
1: yes

ShutdownType can take the following values:

-1: default
0: none (log out)
1: reboot
2: halt (shutdown)
3: logout (same as 0? no idea…)

and finally, ShutdownMode can be:

-1: default
0: schedule
1: try now
2: force now
3: interactive

This is all for Plasma 4, so it may be outdated for Plasma 5. Also I have not noticed any difference with changing the ShutdownMode, further experiments are needed. :man_scientist:t3:

4 Likes