4

In Microsoft Windows I use switch off software for auto shutdown, Hibernate, restart etc. Can I find something similar in Ubuntu?

switch off

I read following link. How can I automatically shutdown the system after a specific time?

5

GShutdown is a utility which allows you to schedule the shutdown or restart of your computer. With it you can simply and quickly choose the turn off time at either a specific time or after a countdown.

Install Gshutdown:

sudo apt-get install gshutdown

enter image description here

source.

0

For some unknown reason this sorts of applications don't last for long and they are broken on newer iterations of Ubuntu. Gshutdown didn't work out of the box for Ubuntu 15.10 and it's not very convenient to download a couple of hundreds MB of dependencies for kwin application. At least not for this kind of application.

Here's a little script I wrote that issues this problem and arguably it is a better choice. Why :

  • it is a graphical script so it can be run as classic application
  • as any other script features can be adjusted to user's preference
  • the code can be used in various distros and it's not probable that will change any time soon
  • various options included are described in script.

Shutdown dialog in action: enter image description here

How to use it

There is only one package that needs to be installed which addresses user inactivity feature, however script will run other available options well without it. To install 'xprintidle' type in terminal:

sudo apt-get install xprintidle

Open text editor, paste the code provided below and save the file as Shutdown_dialog in your home folder. Right click on file and select allow executing file as program or via terminal:

chmod +x ~/Shutdown_dialog

The code:

#!/bin/bash

###########<<INFO
# This script is intended for use in various Linux distros. In order to use all available options install 'xprintidle' package. It is responsible for 'system idle' option,
# but the script will run without it as well. To install package on Debian-based distros execute: `sudo apt-get install xprintidle` .
# Other packages used in script: zenity, notifyOSD, xdotool, wmctrl, play.
#
# Below INFO section there are some predefined variables. Their values can easily be customized by user.
#
#   Some options available:
#   
# -Select an action - the only option that must be selected, others can omit. The hibernation action is not visible by default, but it can be enabled. Use this at your own risk,
# it has not been tested. To enable hibernation uncomment two lines in `case var1` statement and add |Hibernate option in zenity dialogs:
#  --list-values='Shutdown|Restart|Hibernate|Suspend|Logout|Lock screen'. Alternative actions are commented and use them in case your distro doesn't support default ones.
#
# -Time from now; on user inactivity; at specific time and date - only one of these can be used at the same time. The accuracy for latter two is +-1 min.
#
# -Run custom command before action executes - BE CAREFULL !!! with this one. After confirmation dialog timeout, there is no turning back.
#
# -When selected application exit - clicking on this option new dialog will be presented with available options. Select action and preferred options (if any) and press OK. 
# Then select the application by clicking on it.
#
# -Default - first time this option is activated it will open new dialog with available options. Select preferred options. They will be saved in `default_conf_file` 
# which is predefined as    `default_conf_file=~/.cache/Shutdown_dialog_conf_file.txt`  The script will then execute ! 
# But on next run script will execute just by pressing `default` button.
#############INFO

#####Predefined variables intended for user customization
default_conf_file=~/.cache/Shutdown_dialog_conf_file.txt
default_sound_1=/usr/share/sounds/freedesktop/stereo/service-login.oga
default_sound_2=/usr/share/sounds/freedesktop/stereo/dialog-warning.oga
default_sound_3=/usr/share/sounds/freedesktop/stereo/message.oga
defaul_time_to_show_confirmation_dialog=10


#####Functions that correspond to selected options
default_option () {
    if [ -f "$default_conf_file" ];then
        options=$(cat "$default_conf_file")
    else
options=$(zenity --forms --title="Reappeared shutdown dialog" --text="\nYou are running DEFAULT option for the first time. Choose settings\n and press OK. The default options will be saved for further use.\n\n *Only one of the marked options can be used at the same time\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-entry="Time from now (min)*:" --add-entry="On user inactivity (min)*:" --add-entry="At specific time (hh:mm) and date:*" --add-combo="This has no purpose. It only replaces calendar" --combo-values= --forms-date-format=%Y-%m-%d --add-entry="Run command before action executes:" --add-entry="Show desktop notifications every (min):" --add-entry="Seconds to display confirmation dialog:" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3")
        [ $? == 1 ] && exit 1
        echo "$options" > "$default_conf_file"
        rm_default_conf_file="$default_conf_file"
    fi  
}



select_app () {
options=$(zenity --forms --title="Reappeared shutdown dialog" --text="\n You are running WHEN SELECTED APPLICATION EXIT option! \n\n Choose additional settings, press OK and then click on application\n to select. Use Alt+Tab to cycle through open applications.\n\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-combo="Unavailable option" --combo-values= --add-entry="Run command before action executes:" --add-combo="Unavailable option" --combo-values= --add-entry="Seconds to display confirmation dialog:" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3")
    [ $? == 1 ] && exit 1
    win_id=$(xdotool selectwindow)
    notify-send "Window selected:  $(xdotool getwindowname $win_id)"
    win_id=$(printf 0x0%x "$win_id")
    while :
    do
        [[ -z $(wmctrl -l | grep "$win_id") ]] && break || sleep 5
    done
}



user_inactivity () {
    which xprintidle
    if [ $? == 1 ];then
        zenity --warning --timeout=7 --ellipsize --text="\tPackage 'xprintidle' is not installed.\n\n Option ** \"ON USER INACTIVITY\" ** cannot be used!\n\n\t\t Exiting...."
        [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
        exit 1
    fi
    while [[ "$idle_time" -lt "$var3" ]]
    do
        idle_time=$(( $(xprintidle) / 60000 ))
        sleep 5
    done
}



notification () {
    x=1
    [ -z "$time" ] && return 1
    notify-send "System will $var1 in $time minutes!"
    while :
    do
        remaining=$(( $time - $x * $var7 ))
        sleep "$var7"m
        notify-send "System will $var1 in $remaining minutes!"
        x=$(( x + 1 ))
        [ "$remaining" -lt "$var7" ] && break
    done
    sleep "$remaining"m
}


#####Confirmation dialog shown before action is executed
confirmation_dialog () {
    [ -n "$sound" ] && play "$sound" &
    $(sleep 0.9;wmctrl -r "Shutdown confirmation dialog" -b add,above) &        #comment this line if you don't want dialog to be above other windows
    zenity --question --title="Shutdown confirmation dialog" --text="System will $var1 in $var8 seconds! \n\nDo you wish to execute?" --ellipsize --timeout="$var8" --cancel-label=Cancel
    [ $? == 1 ] && exit 1

}


###### Initial Shudown dialog window #######
options=$(zenity --forms --title="Shutdown dialog" --text="\nAvailable options are documented in the header of the script\n\n *Only one of the marked options can be used at the same time\n" --add-list="Select an action:" --list-values='Shutdown|Restart|Suspend|Logout|Lock screen' --add-entry="Time from now (min)*:" --add-entry="On user inactivity (min)*:" --add-entry="At specific time (hh:mm) and date:*" --add-calendar="Select date for previous option" --forms-date-format=%Y-%m-%d --add-entry="Run command before action executes:" --add-entry="Show desktop notifications every (min):" --add-entry="Seconds to display confirmation dialog (s):" --add-combo="Play sound on confirmation dialog:" --combo-values="|default_sound_1|default_sound_2|default_sound_3" --extra-button="When selected application exit" --extra-button="Default")

#####Validate selection and call functions
if [ $? != 0 ];then
    if [ "$options" == "When selected application exit" ];then select_app
    elif [ "$options" == "Default" ];then default_option
    else exit 1
    fi
fi

#####Variables assignment
old="$IFS"
IFS="|"
read -r var{1..9} <<< "$(echo "$options")"
IFS="$old"

#####Validate and define time variables
if [ "$var2" != 0 ] && [ -z "$var3" ] && [ -z "$var4" ]; then 
    time="$var2"
elif [ "$vr3" != 0 ] && [ -z "$var2" ] && [ -z "$var4" ]; then
    if [ ! -z "$var7" ];then
        zenity --warning --timeout=7 --ellipsize --text="\nThe *** \"ON USER INACTIVITY\" *** option cannot be used with desktop notifications!\n\n\t\t Exiting...."
        [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
        exit 1
    fi
    user_inactivity
elif [ "$vr4" != 0 ] && [ -z "$var3" ] && [ -z "$var2" ]; then
    current=$(date +%s)
    target=$(date +%s -d "$var5 $var4")
    time=$(( ($target - $current) / 60 ))
elif [ "$var2" == " " ] && [ "$var3" == " " ] && [ "$var4" == " " ]; then :
else
    zenity --warning --timeout=5 --ellipsize --text="\t*Only one of the marked options can be used at the same time!\n\n\t\t Exiting...."
    [ -f "$rm_default_conf_file" ] && rm "$default_conf_file"
    exit 1  
fi

#####Validate selection and define action (replace it with commented one if it doesn't work)
case $var1 in
    Shutdown,)
        action="poweroff";;                     #"systemctl poweroff"
    Restart,)
        action="reboot";;                       #"systemctl reboot"
#   Hibernate,)
#       action="systemctl hibernate";;
    Suspend,)
        action="systemctl suspend";;
    Logout,)
        action="gnome-session-quit --logout --no-prompt";;      #"pkill -u $USER"
    Lock?screen,)
        action="gnome-screensaver-command -l";;             #"xdotool key Ctrl+Alt+l"
esac

#####Validate other settings
[ -n "$var7" ] && notification

[ -z "$var8" ] && var8="$defaul_time_to_show_confirmation_dialog"

[ "$var9" != 0 ] && case $var9 in
    default_sound_1)
        sound="$default_sound_1";;
    default_sound_2)
        sound="$default_sound_2";;  
    default_sound_3)
        sound="$default_sound_3";;
esac    

#####Execute
[[ -n "$time" && -z "$var7" ]] && sleep "$time"m
confirmation_dialog
eval "$var6"
eval "$action"

exit 0

Optionally, you can create desktop icon. Open text editor, paste the code provided below and save the file as Shutdown_dialog.desktop in your desktop folder. Right click on file and select allow executing file as program or via terminal:

chmod +x ~/Desktop/Shutdown_dialog.desktop

The code:

#!/usr/bin/env xdg-open

[Desktop Entry]
Name=Shutdown dialog
Comment=Shutdown dialog script
Terminal=false
Icon=/usr/share/icons/oxygen/64x64/actions/system-shutdown.png
Exec="/home/username/Shutdown_dialog"
Type=Application
Categories=Application;Utility;
Name[en_US]=Shutdown dialog

Just make sure that Exec line have an absolute path to the script.

If anything is unclear, comment and I will edit if necessary.

0

I think GShutdown is the best among this utilities. But to make it work with Ubuntu 15.10 you need to do some trick.

  1. install & open gshutdown
  2. Edit -> Preferences
  3. Select "Custom Command"
  4. Click "Turn off the computer" and paste this sh -c "dbus-send --system --print-reply --dest=org.freedesktop.login1 /org/freedesktop/login1 "org.freedesktop.login1.Manager.PowerOff" boolean:true"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.