Logout, reboot and shutdown using the terminal

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.

13 Likes

I actually happened to be browsing the ksmserver code recently and it appears that this was a mistake and can be ignored. It’ll be removed in Plasma 6.

3 Likes

OOrrrrr… :rofl:

4 Likes

Using systemclt reboot and systemctl poweroff with Plasma is not ideal, because it doesn’t log you out of your Plasma session, so it does not execute all the logout stuff Plasma does, like saving the current session (if you have that enabled) and running any scripts you have set to run on shutdown.

It is faster, though, because of that.

1 Like

True. Like you said, I don’t do session save. It’s something I put together once, years ago, since I’m actually an openbox nut and I needed something quick. Nevertheless, been using it in plasma for a good while for ease of use and moreover, no typing. Hate keyboard stuff. Was just foolin’ around. I’ll delete it if you like.

1 Like

You could modify your script by replacing:

loginctl terminate-user john → qdbus org.kde.ksmserver /KSMServer logout 0 0 1
systemctl reboot             → qdbus org.kde.ksmserver /KSMServer logout 0 1 1
systemctl poweroff           → qdbus org.kde.ksmserver /KSMServer logout 0 2 1

Or you can leave it as it is, I don’t think there is any problem with it, except that it doesn’t properly log you out of your Plasma session.

2 Likes

Thanks. I’ll give it a swing. Maybe I’ll try something else instead of ol’ man style zenity. Got jgmenu installed, might as well use a second, rofi styled, one with those commands. :beers:

2 Likes

It seems there’s no KSMServer option than can be used instead of the command systemctl reboot --firmware-setup?

When I want to log out of my Plasma session, I run a command:

Please don’t use that.

 qdbus org.kde.LogoutPrompt /LogoutPrompt  org.kde.LogoutPrompt.promptLogout
 qdbus org.kde.LogoutPrompt /LogoutPrompt  promptReboot
qdbus org.kde.LogoutPrompt /LogoutPrompt  promptShutdown

to get a prompt

or

qdbus org.kde.Shutdown /Shutdown  org.kde.Shutdown.logout

logoutAndReboot
logoutAndShutdown

to perform an action without prompt

This works in Plasma 5 since a few years, the ksmserver API will be gone in Plasma 6. I didn’t get rid of it at the time given how much the old API has been copy pasted about the internet.

13 Likes

I need to enter UEFI Firmware Settings from time to time, and I find the current KDE Plasma way of doing this even less accessible than Windows:

System Settings > Startup and Shutdown > Desktop Session > After next restart: Enter UEFI setup screen

I think there must be a shorter way of doing this like

qdbus org.kde.Shutdown /Shutdown org.kde.Shutdown.logoutAndUEFI

or something like it.
As I don’t know this, I currently use

systemctl reboot --firmware-setup

which is not the best way…

If you just want to do a quick logout I use pkill -u $USER instant logout and it is short command :innocent:

I would absolutely not recommend this, since this means all processes of that user session would be abruptly terminated. Doing so leads to an increased risk of data loss (e.g. of your unsaved work in some app) or data corruption.

Many bootloaders (e.g. systemd-boot, refind) have a menu item for that. So you don’t need to do it from Windows or Plasma.

Properly-written software (e.g. most text editors) shouldn’t lose data on kill (or sudden poweroff). Unfortunately seems most KDE background services aren’t properly written. They only save states on clean exit, instead of on every change or periodically.

The thing is, you just can’t rely on that, at least not without testing or knowing beforehand. Made a quick test:

  • Kate indeed intercepts that signal and asks what you want to do with your unsaved work
  • Libreoffice just gets killed. It has an internal recovery mechanism which in my test case failed to recover anything.
  • Gimp just gets killed. It also has an internal recovery mechanism that succeeded in restoring the last image state (but not the undo stack).
  • Inkscape just gets killed. It didn’t restore any unsave work and there was no hint that it could do so.
  • Audacity just gets killed. Like Gimp, it has an internal recovery mechanism and was able to restore the latest file state (but without the undo stack).

So you might have luck that your app protects your data against this case of an “hard shutdown”, but you would need to know/test which of your apps does that and how reliable it actually is.

No, it’s not about unsaved changes. You won’t have any chance with unsaved changes on a sudden poweroff, anyway. And if you do a pkill -u $USER, probably you already saved all you like to save.

It’s about saving configuration changes as soon as possible. For example, plasmashell seems to only save applet states on exit and on adding/removing applets. So if you pin an app to the task manager, or drag-sort their orders, that is only saved on logout, and all changes are lost when plasmashell crashes. And in the 6.0 beta, plasmashell crashes all the time, so it never gets saved at all.

For Kate if you have the option to auto save your work enabled it will save it as soon as it is no longer focused when you go to the terminal to issue the command to do what you wish. Even does the save when switching between tabs in it.

1 Like

Just make sure you name your file at the beginning so it knows to save it. :slightly_smiling_face:

Possibly matters as I have never experienced data loss due to it not saving the draft at that point file when I have use the terminal for restart/shutdown/logout. It is always there on the next login waiting for you to resume what you were doing. At least it is how it works for me, YMMV.

Can you give us an update on where this is in KDE6 / Plasma 6 at this point?

I’m not finding org.kde. LogoutPrompt nor Shutdown nor similar under Ksmserver.

I have a script that switched my grub config to other boot options, then opens the Logout screen, and I can’t figure out how to get this Logout prompt in KDE6.

To be clear, I want the logout screen, pre-selected to reboot, not an unprompted instant reboot.

I was previously using the following:

qdbus org.kde.ksmserver /KSMServer logout 1 1 3

Also, if there is documentation on the dbus commands you could point me to, that would be awesome, otherwise I assume the source code is where I’d need to look for further details?

Thanks!

4 Likes