You can send a logout command via dbus to KDE, which should (if it can) terminate the session.
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.