5

I have a newly installed Ubuntu 16.04 (with Unity) on a laptop and I would like to be able to launch the dialogue window (the one that is by default launched by pressing and shortly holding the power button, with four buttons to lock, suspend, reboot and halt the system respectively) with a custom keyboard shortcut.

The purpose of this is to be able to launch the dialogue with external (either USB or BT) keyboards when the laptop lid is closed and an external display is used instead, rendering power button inaccessible or inconvenient at the very least.

With old Ubuntu 12.04 I used this simple command:

exec /usr/lib/indicator-session/gtk-logout-helper --shutdown

to which I bound the Ctrl-Alt-Del keyboard shortcut (Log Off I redirected to Ctrl-Shift-Del).

Is there analogical command in Ubuntu 16.04 (or is this just another thing that was “improved” to worse, as so many seem to be)?

Thanks a lot in advance for any help!

4
  • Note: I am well aware I can create my own dialogue with zenity &c., however I'd like to stay with standard means if at all possible.
    – mpts.cz
    Aug 21, 2016 at 11:21
  • Yes, there is command for that. I am currently on mobile so please wait a few minutes Aug 21, 2016 at 11:42
  • @Serg: Even few and half… :-)
    – mpts.cz
    Aug 21, 2016 at 11:45
  • Hi @mpts.cz Please see my answer. Am I missing something? Aug 21, 2016 at 15:12

3 Answers 3

9

In general, the dialogs for logout, reboot, and shutdown can be launched via dbus. In particular, what you want is

qdbus com.canonical.Unity  /com/canonical/Unity/Session com.canonical.Unity.Session.RequestShutdown

You can list other methods via this command:

$ qdbus com.canonical.Unity  /com/canonical/Unity/Session | grep '\.Request.*'                                           
method void com.canonical.Unity.Session.RequestLogout()
method void com.canonical.Unity.Session.RequestReboot()
method void com.canonical.Unity.Session.RequestShutdown()

I have used this same approach for multiple other answers, for instance

How to get warning for "Suspend"


In case someone feels the command is a bit lengthy, remember Linux 101 : you can create aliases for commands or functions.

alias quit_session='qdbus com.canonical.Unity  /com/canonical/Unity/Session com.canonical.Unity.Session.RequestShutdown'

quit_session()
{
    qdbus com.canonical.Unity  \
          /com/canonical/Unity/Session \
          com.canonical.Unity.Session.RequestShutdown
}

On command line this will be called as quit_session . Easy, right ? You can place this into ~/.bashrc. If it is still lengthy , use even shorter name.

Despite the length it does exactly what is asked in the question.

4
  • Thank you Serg, that is exactly what I wanted. Just out of curiosity, is there some documentation to this available? Both man qdbus and quick googling seem to offer rather terse info…
    – mpts.cz
    Aug 21, 2016 at 12:09
  • @mpts.cz qdbus is a tool for accessing dbus service, so there's going to be only documentation about that tool. dbus itself has tons and tons of services , each depending on application. There is a lot of general information online , but nothing that covers all aspects. I basically spend time playing around with it and googling here and there to figure out what works, what doesn't , so can't really suggest any specific documentation source - it's just something i learned over time. Aug 21, 2016 at 12:15
  • Hi Serg. I might be missing something, but see my answer. Why this complicated? Aug 21, 2016 at 15:06
  • Length of the text does not mean complexity. It is easily shortened as alias or function, if you need to call it from command line. Besides, underneath the hood your answer gnome-session-quit issues a call to dbus interface as well. On Unity however that interface isn't implemented and goes back to call the interface and method i listed in my answer. so, no, it's not complex, just. . . . different Aug 21, 2016 at 18:19
9

The command:

gnome-session-quit --power-off

simply works, and does exactly what you want:

enter image description here

From man gnome-session-quit:

OPTIONS
       The following options are supported:

       --logout
              Prompt the user to confirm logout. This is the default behavior.

       --power-off
              Prompt the user to confirm system power off.

       --reboot
              Prompt the user to confirm system reboot.

       --force
              Ignore any inhibitors.

       --no-prompt
              End the session without user interaction. This only  works  with
              --logout.
0
4

Serg's answer gives a Unity-specific way of achieving this. Here's a general X11 way (requires xdotool package installed):

xdotool key XF86PowerOff

This worked for me out of the box in KDE and XFCE. Most modern DEs can be configured to treat Power key on the keyboard in a similar fashion. Even if your keyboard doesn't have such a key, the keysym is still available to Xorg and will work.

5
  • You might want to add that xdotool is not installed by default :) Aug 21, 2016 at 15:54
  • 2
    @JacobVlijm qdbus isn't either, at least on Xubuntu.
    – Ruslan
    Aug 21, 2016 at 16:25
  • I believe you, but the answer won't work for anyone without xdotool. The majority I believe. Just wanted to do a suggestion for your answer to improve. Aug 21, 2016 at 16:36
  • @JacobVlijm edited
    – Ruslan
    Aug 21, 2016 at 18:07
  • 2
    We'll, qdbus comes with Unity by default, and op said ` I have a newly installed Ubuntu 16.04 (with Unity)` , so my answer is a bit more suitable for their case. Plus, this technically is emulation of keypress, and if power key is remapped or disables, this might not word. Nonetheless, it's a creative approach. But Jacob is correct - please mentioned that xdotool requires installation. It's not a critique, it's suggestion for improvement Aug 21, 2016 at 18:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .