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!

mpts.cz
  • 213
  • 1
  • 8

3 Answers3

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.

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.
Jacob Vlijm
  • 85,475
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.

Ruslan
  • 1,863