Ubuntu – Command to launch poweroff/reboot dialogue

16.04shortcut-keysshutdownsuspendunity

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!

Best Answer

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.