How can I safely shutdown/reboot/logout KDE from the command line?

30,519

Solution 1

For KDE 5+:

qdbus org.kde.Shutdown /Shutdown logout
qdbus org.kde.Shutdown /Shutdown logoutAndReboot
qdbus org.kde.Shutdown /Shutdown logoutAndShutdown

The last option specifies which method gets called. It seems the options for KDE4 mentioned below are partially supported(reboot didnt work so I ended up using these newer methods).

Ref: Reddit

For KDE 4:

Note that this answer was written in 2010 for KDE 4. It may not apply to modern systems.

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

The three integer parameters are the confirm, sdtype and sdmode arguments to KWorkSpace::requestShutDown. Their values are explained at the top of the page. Since the page has disappeared, here are the values (still present in a cache).

enum ShutdownConfirm {
  ShutdownConfirmDefault = -1,
  ShutdownConfirmNo = 0,
  ShutdownConfirmYes = 1
}
  • ShutdownConfirmDefault: Obey the user's confirmation setting.
  • ShutdownConfirmNo: Don't confirm, shutdown without asking.
  • ShutdownConfirmYes: Always confirm, ask even if the user turned it off.
enum ShutdownMode {
  ShutdownModeDefault = -1,
  ShutdownModeSchedule = 0,
  ShutdownModeTryNow = 1,
  ShutdownModeForceNow = 2,
  ShutdownModeInteractive = 3
}
  • ShutdownModeDefault: Select previous mode or the default if it's the first time.
  • ShutdownModeSchedule: Schedule a shutdown (halt or reboot) for the time all active sessions have exited.
  • ShutdownModeTryNow: Shut down, if no sessions are active. Otherwise do nothing.
  • ShutdownModeForceNow: Force shutdown. Kill any possibly active sessions.
  • ShutdownModeInteractive: Pop up a dialog asking the user what to do if sessions are still active.
enum ShutdownType {
  ShutdownTypeDefault = -1,
  ShutdownTypeNone = 0,
  ShutdownTypeReboot = 1,
  ShutdownTypeHalt = 2,
  ShutdownTypeLogout = 3
}
  • ShutdownTypeDefault: Select previous action or the default if it's the first time.
  • ShutdownTypeNone: Only log out.
  • ShutdownTypeReboot: Log out and reboot the machine.
  • ShutdownTypeHalt: Log out and halt the machine.
  • ShutdownTypeLogout: Temporary brain damage. Don't use. Same as ShutdownTypeNone

Solution 2

So I tried the answer presented by Gilles, but that only works for KDE4.

After a system-update with my graphics, I could no longer log out, reboot, or shutdown. Eventually found this command worked:

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

My source is from here, where they discuss it a bit more. I'm not sure about he other optoins. Forum topic discussion KDE5 shutdown options. The above command seems to have shutdown my system gracefully. All my programs came back that were expected, in the right layout order, and I did not seem to be missing anything. If this doesn't work, please comment and I will adjust my answer but so far this is all that has worked for a graceful KDE5 shutdown when my is locked. (I obviously could have used the shutdown command or called init, but those aren't graceful.)

Solution 3

For any version of KDE (maybe also GNOME and others):

In KDE Control Center (KDE3.5/trinity) you can set a keyboard shortcut for "halt without confirmation" (should be a complicated one to avoid tragedy, like Ctrl+Shift+Alt+Delete) and then run xvkbd (virtual keyboard):

xvkbd -text '\C\S\A\d'

You can create an alias for this (e.g. kdehalt);

My favourite one: sleep 1h 20m && kdehalt or wget "http://something" ; kdehalt.

Share:
30,519

Related videos on Youtube

txwikinger
Author by

txwikinger

Ubuntu member, Kubuntu member

Updated on September 17, 2022

Comments

  • txwikinger
    txwikinger almost 2 years

    I am not talking about shutdown and reboot commands. I want to initiate the same routine from command line that would be performed if I would press the logout/reboot/shutdown button inside the KDE desktop.

    • Admin
      Admin almost 14 years
      I dont know if you could do that. one of the commands would eventually close the terminal your using to interact with it. Maybe more ideas on why you need to do this? :)
    • Admin
      Admin almost 14 years
      I need to do this because plasma-desktop is acting up sometimes and I still want to keep the session information that is saved during logout
  • user3099609
    user3099609 over 10 years
    Gilles' answer helped me as well. Just a small note there: it doesn't work from su. In my case this was part of a command issued using sudo. In that case it doesn't work. Complains Cannot find 'org.kde.KSMServerInterface.logout' in object /KSMServer at org.kde.ksmserver. However works very well when run with the regular user ^_^
  • kdb
    kdb almost 4 years
    The linked webpage is no longer accessible, nor could I find any replacement or web.archive.org entry. From reddit: Confirm: "-1": default user config, "0": No confirmation windows, "1": Always with confirmation window. sdtype: "-1": default user config, "0": logout, "1": reboot, "2": halt. sdmode: "-1": default user config, "0": shutdown after all session have ended, "1": shutdown if no active sessions, "2": force shutdown, "3" ask user what to do if sessions are still active.
  • h3.
    h3. almost 4 years
    @kdb I found that page still in a Google cache, but that probably won't last long. I've copied the relevant part into my answer.
  • kdb
    kdb almost 4 years
    Curiously, org.kde.KSMServerInterface.logout did not work for me, but I managed to reboot with qdbus org.kde.ksmserver /KSMServer logout 0 1 2 (see C.D.'s answer).
  • h3.
    h3. almost 4 years
    @kdb The KDE APIs have changed in the last 10 years. C.D.'s answer applies to more recent versions of KDE than mine.
  • Carlos Pinzón
    Carlos Pinzón over 3 years
    Could you please sort your explanation in the same order of the arguments? It improves readability: (i) ShutdownConfirm, (ii) ShutdownType and (iii) ShutdownMode correspoinding to (i) confirm, (ii) sdtype and (iii) sdmode