To end a KDE session from the shell without root privileges what you can do is send a logout command via dbus to KDE. This then should terminate the session.
The command is as follows:
qdbus org.kde.ksmserver /KSMServer logout 0 0 0
dbus is a messaging system that lets applications communicate with each other, and the qdbus command is a utility for sending dbus messages to applications.
Applications register with dbus, and the ksmserver part of KDE is the session manager – it looks after who is logged in.
So we are sending a message to ksmserver to the /KSMServer interface and telling it to logout. The message we are sending is the exact same message that is sent to KSM when you click on the logout icon on your desktop.
The three zeros are parameters and can alter the type of logout we are doing:
0 = Do not wait for confirmation
1 = Wait for confirmation (with a 30 second timeout)
-1 = Prompt for action, defaulting to shutdown (Only makes sense when waiting for confirmation.)
0 = Logout
1 = Restart
2 = Shutdown
The third parameter is the “when” parameter, but it isn’t clear what its impact is.
This is especially useful for when you are having an issue with your session and by doing this, prevents you from having to do a force shutdown as once you’ve terminated the session, you can start a new one from the terminal.