Thursday, September 22, 2011

Shutdown Timer Tool for Linux

If you want to shutdown your system at a given time or - as covered by this tool - after a given period of time, you usually have to use the command line. But with this little script, you can both set and cancel a shutdown timer through a GUI, run from a launcher, of course.


The script is tested under Gnome in Ubuntu, but it should work in most other Linux distros and desktop environments as well.

Create the Script

1. In the Terminal, enter:

gksudo gedit /usr/local/bin/shutdown-timer

2. Paste this into the text editor:

#!/bin/bash
#
# Shutdown Timer Tool for Linux
#
# Depends on zenity
#
# Author: Krytarik
#

if [ -n "`ps ax |grep shutdown |egrep -v 'grep|shutdown-timer'`" ]; then
    zenity --title="Shutdown Timer" --question --text="Cancel Shutdown Timer?"
    if [ "$?" = "0" ]; then
        sudo shutdown -c
        if [ "$?" = "0" ]; then
            zenity --title="Shutdown Timer" --info --text="Shutdown cancelled."
        else
            PASSWORD=`zenity --title='Shutdown Timer' --entry --text='Please enter your password:' --hide-text`
            if [ -n "$PASSWORD" ]; then
                echo $PASSWORD | sudo -S shutdown -c
                if [ "$?" = "0" ]; then
                    zenity --title="Shutdown Timer" --info --text="Shutdown cancelled."
                else
                    zenity --title="Shutdown Timer" --error --text="Sorry, wrong password!"
                fi
            fi
        fi
    fi
else
    TIMER=$(zenity --title="Shutdown Timer" --scale --text="Set Shutdown Timer (in minutes):" --value=60 --min-value=5 --max-value=180 --step=5)
    if [ -n "$TIMER" ]; then
        sudo -b shutdown -h +$TIMER
        if [ "$?" = "0" ]; then
            zenity --title="Shutdown Timer" --info --text="Shutdown in $TIMER minutes!"
        else
            PASSWORD=`zenity --title='Shutdown Timer' --entry --text='Please enter your password:' --hide-text`
            if [ -n "$PASSWORD" ]; then
                echo $PASSWORD | sudo -Sb shutdown -h +$TIMER
                if [ "$?" = "0" ]; then
                    zenity --title="Shutdown Timer" --info --text="Shutdown in $TIMER minutes!"
                else
                    zenity --title="Shutdown Timer" --error --text="Sorry, wrong password!"
                fi
            fi
        fi
    fi
fi

3. You can change the values highlighted in the above code to your needs if you like. Those options are pretty much self-explanatory, but notice that the "--step" option only affects the stepping when using the Arrow keys.

4. Save the file, quit.

5. Make the script executable:

sudo chmod +x /usr/local/bin/shutdown-timer

Create the Launcher

1. In the Terminal, enter:

gksudo gedit /usr/share/applications/shutdown-timer.desktop

2. Paste this into the text editor:

[Desktop Entry]
Name=Shutdown Timer
Comment=Set or Cancel a Shutdown Timer
Exec=shutdown-timer
Icon=system-shutdown
Terminal=false
Type=Application
Categories=Utility;

3. Save the file, quit.

4. Update the menu item cache (otherwise done by dpkg during a usual installation/upgrade):

sudo rm /usr/share/applications/desktop.*.cache
sudo sh -c "/usr/share/gnome-menus/update-gnome-menus-cache /usr/share/applications/ > /usr/share/applications/desktop.${LANG}.cache"

Note: This last step can't be done in Oneiric 11.10, as it doesn't include those tool in the first place, and apparently doesn't do those caching anymore. There you just need to relogin for the new menu item to show up.

Usage

After the installation, you will find "Shutdown Timer" in "Applications > Accessories" in classic Gnome, or by typing its name into the Unity Dash.

Whether you want to set or cancel a shutdown timer, you just click on the launcher. If a shutdown timer is currently running, you get the option to cancel those, if not, you get the option to set one.




Disable Password Query

This step is optional!

If you don't want to enter your password when using this tool - more precisely, disable the system's password query for running "/sbin/shutdown" with 'sudo' -, follow these instructions.

1. Open "/etc/sudoers" for editing with 'visudo':

sudo visudo

2. At the very bottom of the file, add this line to disable the password query for all members of the "admin" group:

%admin ALL=(ALL) NOPASSWD: /sbin/shutdown

If you only want to disable it for your or another specific user, just replace "%admin" with the respective username.

You can see this guide for more infos and options reg. the "sudoers" file:

https://help.ubuntu.com/community/Sudoers

3. Press Ctrl+O to save the file.

4. Press Ctrl+X to exit the editor.

Related Posts:

System