Ubuntu – How to call gnome-session-quit with countdown from Unity

gnome-classicshutdownunity

To be able to shutdown with a keyboard shortcut we can assign gnome-session-quit ---power-off to a custom shortcut.

In Unity this will lead to the following dialog:

enter image description here

Then we need another at least two keystrokes to finally power off our system. This is rather inconvenient and I would prefer the old shutdown dialog when you could poweroff by just pressing Return or letting it wait the default countdown of 60 seconds.

When calling gnome-session-quit --poweroff from a GNOME session flashback session on the same system (14.04 LTS) the old dialog including the countdown comes back:

enter image description here

So we know it dwells somewhere.

Is there any way to call this old dialog when running a Unity session?

Best Answer

Here's a script to emulate the desired behavior. Must be ran as with sudo. Can be bound to a keyboard shortcut ( with preliminary addition of the shutdown command to sudoers file to allow passwordless run ). Simplistic, concise, and does the job.

#!/bin/bash
# Date: June 11,2015
# Author: Serg Kolo
# Description: a script to emulate
# behavior of GNOME session flashback
# shutdown dialog

# Tell ubuntu to shutdown in 1 min
shutdown -P +1 &
# Show the dialog
zenity --question --text="Shutdown now ? Automatic shutdown in 60 seconds" --ok-label="DOIT" 
# If user clicks DOIT, then cancel the old 
# shutdown call that has countdown,
# (because only one shutdown command can be run at a time), and
# tell ubuntu to shutdown immediately
# otherwise - cancel it
if [ $? -eq 0 ];then
        shutdown -c
        shutdown -P now
else
        shutdown -c
fi

Update: June 14

As suggested by Takkat, here's a script that utilizes zenity's --timer option and dbus to achieve the same behavior without need for sudo access:

#!/bin/bash
# Date: June 14,2015
# Author: Serg Kolo
# Description: a script to emulate
# behavior of GNOME session flashback
# shutdown dialog
# version #2

zenity --question --text="Shutdown now ? Autoshutdown in 60 seconds" \
    --cancel-label="DOIT" --ok-label="NOPE" --timeout=60 ||  
  dbus-send --system --print-reply --dest=org.freedesktop.login1 \
    /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true

Basic idea here is that zenity's timeout option exits with code greater than 0, which typically means command failed. So by treating zenity's cancel option and timeout as the condition that will allow shutdown, we use the OR operator (|| ) to shutdown only if user clicks the cancel button (labeled as "DOIT" ) or dialog times out.

Another variation to improve user experience can be done with yad (needs to be installed first with these commands sudo apt-add-repository ppa:webupd8team/y-ppa-manager;sudo apt-get update; sudo apg-get install yad ). This variation uses progress bar to let user know how much time is left

    #!/bin/bash
    yad --auto-close --sticky --on-top --skip-taskbar --center \
  --text 'Shutdown now ? Autoshutdown in 60 seconds.' \
  --button="gtk-ok:1" --button="gtk-close:0" --image=dialog-question \ 
--title 'Shutdown' --timeout=60 --timeout-indicator=top || 
dbus-send --system --print-reply --dest=org.freedesktop.login1 \
/org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true

Another possible version, takes into account that if you change zenity's ok button label, the button highlighted by default may or may not be the ok button.

zenity --question --timeout 10 --text="Automatic shutdown in 10 seconds"
if [[ $? -eq 1 ]] ; then
    # user clicked Cancel
    exit 
else
    dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true
fi

The script shuts down the system upon any return that is not 0. If script times out , the return value of either 1 or 5 tells the script to execute the else part

Related Question