Adding an Auto Shutdown Feature to the End of a Script

Whole Script

# Main Script Function ...

# Shutdown Option
export DISPLAY=:0.0;
if [ `/usr/bin/xprintidle` -gt 60000 ]; then
    /usr/bin/zenity --question --title="Job Completed" --timeout=60 \
                    --text="Job Completed; Proceed to Shutdown?" \
                    --ok-label="Yes" --cancel-label="No";
    if [ "$?" -eq "5" ]; then
        # Proceed to Shutdown
        /sbin/shutdown -h now;
    fi
fi

Explanations

export DISPLAY=:0.0;

This is necessary if the script is run outside of an X session (e.g. as a cron job), so that xprintidle can get the correct X session idle time, and zenity can show the dialog on the correct X server display.

if [ `/usr/bin/xprintidle` -gt 60000 ]; then

Gets the X session idle time, and presents the shutdown option if the X session has been inactive for some time (in this case, 60000 milliseconds). Otherwise, it is assumed that the user is active and does not want the system to shutdown.

/usr/bin/zenity --question --title="Job Completed" --timeout=60 \
                --text="Job Completed; Proceed to Shutdown?" \
                --ok-label="Yes" --cancel-label="No";

Displays the shutdown option dialog with the specified title, text, and options. If none of the options is selected within the timeout period (in this case, 60 seconds), the dialog closes with the “Yes” option selected by default.

if [ "$?" -eq "5" ]; then

Shuts down the system if the “Yes” option was selected, or if no option was selected within the timeout period (hence defaulting to the “Yes” option). Otherwise (i.e. the “No” option was selected within the timeout period), the system does not shut down.

/sbin/shutdown -h now;

Shuts down the system.

Note: xprintidle and zenity may not be present in a default installation. They can be installed through the package manager.

References

[1] https://www.linuxquestions.org/questions/linux-newbie-8/export-display%3D-0-0-a-682926/
[2] http://freecode.com/projects/xprintidle
[3] https://help.gnome.org/users/zenity/stable/

Installing Raspbian on the Raspberry Pi

Download Image

1. Download the latest Raspbian release (Wheezy) from http://www.raspberrypi.org/downloads (e.g. 2013-05-25-wheezy-raspbian.zip).

2. Check that the hash of the downloaded file matches the one on the download site.

$ sha1sum 2013-05-25-wheezy-raspbian.zip
131f2810b1871a032dd6d1482dfba10964b43bd2  2013-05-25-wheezy-raspbian.zip

Write Image to SD Card

3. Extract the SD card image file from the archive.

$ unzip 2013-05-25-wheezy-raspbian.zip

4. Write the image to the SD card.

$ sudo dd bs=4M if=2013-05-25-wheezy-raspbian.img of=/dev/sdc

where /dev/sdc is the path to the SD card device.
IMPORTANT: The path varies; using the wrong path would cause the wrong disk to be overwritten.

Resize Root Partition on SD Card

5. Run parted on /dev/sdc.

$ sudo parted /dev/sdc

6. Print the partition table.

(parted) unit chs
(parted) print
Model: Generic STORAGE DEVICE (scsi)
Disk /dev/sdc: 482,18,47
Sector size (logical/physical): 512B/512B
BIOS cylinder,head,sector geometry: 482,255,63.  Each cylinder is 8225kB.
Partition Table: msdos

Number  Start     End         Type     File system  Flags
 1      0,130,2   7,165,29    primary  fat16        lba
 2      7,165,30  235,214,42  primary  ext4

7. Remove the existing root partition.

(parted) rm 2

8. Re-create the partition, filling the entire disk.

(parted) mkpart primary 7,165,30 482,18,47
(parted) quit

where 7,165,30 is the starting chs (cylinder, head, sector) number of the original (deleted) partition, and 482,18,47 is the ending chs number of the disk. The information is available in the original partition table.

9. Check and repair the root partition.

$ sudo e2fsck -f /dev/sdc2

10. Resize the root partition.

$ sudo resize2fs /dev/sdc2

Login to Raspberry Pi

11. Insert the SD card into the Raspberry Pi and switch it on.

12. Access the Raspberry Pi remotely using SSH.

$ ssh pi@192.168.1.2

where 192.168.1.2 is the IP address assigned to the device. The default username is pi, and the password is raspberry.

References

[1] http://www.raspberrypi.org/downloads
[2] http://elinux.org/RPi_Easy_SD_Card_Setup
[3] http://elinux.org/RPi_Resize_Flash_Partitions