Save session using shell command in KDE

Solution 1:

If you're looking to save what the Session Management control module calls the "manually saved session":

qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession

If you're looking to overwrite the session that normally gets saved upon logout:

qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSessionAs "saved at previous logout"

Get a list of existing sessions:

qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.sessionList
(Notable session names are "saved by user" and "saved at previous logout")

You can find more interesting commands if you omit the method from the command as such:

qdbus org.kde.ksmserver /KSMServer

Session data is stored in:

  • KDE 4: ~/.kde4/share/config/ksmserverrc
  • KDE 5: ~/.config/ksmserverrc

Solution 2:

dbus-send --dest=org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession will save your session. When you want to restore it, run ksmserver, which should automatically load the session.

(Please note there is a space between \KSMServer and org.kde.KSMServerInterface.saveCurrentSession, they should all be in the same terminal command)

More information from here: http://raetsel.wordpress.com/2008/04/25/kde4-save-current-session-dbus-to-the-rescue/

Solution 3:

The KDE start menu button saves session with name "saved by user" and this is restored at login if and only if you have selected restore manually saved session in settings.

To save this type of session from command line, command DISPLAY=:0.0 qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSession will do (finding correct dbus session is a bit more complex trick for this answer).

Above doesn't however save session on logout. With option to restore session saved on logout, one would presume command qdbus org.kde.ksmserver /KSMServer org.kde.KSMServerInterface.saveCurrentSessionAs "saved at previous logout" would work, but it doesn't. Ksmserver doesn't allow overwriting this special session (refer to this line in source code).

There is kde bug related to this later case that does provide hints for a workaround till the bug is fixed (might take a lot of time).