My workflow is usually very terminal-centric, I prefer to type, rather than click on things. I spend most of my day using Konsole.
When I want to log out of my Plasma session, I run a command:
qdbus org.kde.ksmserver /KSMServer logout 0 0 1
If you replace 0 0 1 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.
Here is what the three numbers do. They are in this order:
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
So, if you run the above command with, for example 0 1 1, this will restart your computer without any prompt (if you want a prompt, use 1 1 1). That’s similar to running the systemctl reboot command, but better, because it gracefully ends your Plasma session and logs you out before rebooting.
Similarly, 0 2 1 will shut down your computer, similar to shutdown now command, but again, logging you out from your plasma session.
Of course, that qdbus command is a lot of typing, so I use an alias.
This is how you can set it up. 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.
All of this is, of course, applicable to any scripts as well.
