Sunday, March 10, 2013

Shutdown Linux nicely with a USB stick


This post is sort obsolete since there is another way to shutdown linux nicely that requires less of everything. You can read about it here. However maybe you came for the script that looks for a file and issues a command. Well then you're in the right place, just scroll down a bit.

Shutdown Linux nicely with a USB stick

If I had such a thing like a following from regular readers you would probably know about my ancient Toshiba Satellite 320CDS laptop that now serves as a digital picture frame. The only problem is to have it shut down properly.

Why is that a problem you ask?
Well, I can't use the power button as it just cuts the power and I risk getting disk errors that most likely forces me to hook up a keyboard on the next boot. Something that I rather avoid.
But I can do it via a ssh connection. However that requires a working network connection and another device. That leaves me with the option I'd rather avoid. Connecting a keyboard and manually shutting the system down. Ok, not the end of the world but it's a bit tedious to have to drag out a keyboard each time I want to do a shut down.

My solution for this is a script that checks if a certain file is present in a defined directory. If the file is present the script issues the "shutdown -h now" command. The physical part of this is that the file is contained on a USB stick that when inserted automounts to a certain location. By running the script from root's crontab I now have a way to make Linux shut down nicely without a working network connection or keyboard.

Howto: Shutdown Linux nicely with a USB stick 

So what is needed for this to work? Well first of all here's my script. It's not very complicated and you should be able to tailor it according to your needs. Perhaps you want to run a slideshow with the pictures on a USB stick when inserted or maybe even completely wipe your system.

The next step is to make a USB stick automount in a certain directory.
For this we need to install autofs. There's good tutorial on this here and here, but I'll give you the basics below.

Since I run debian installation is as easy as:
$ sudo apt-get install autofs 

To configure autofs I started by adding this line to /etc/auto.master
/media   /etc/auto.removable     --timeout=2

Then I added this line to /etc/auto.removable
usb-shutdown -fstype=ext2 UUID=UUIDOFYOURDEVICE

The UUID is the Universally unique identifier of a disk. In this case we use it as way to know what USB stick to mount in /media/usb-shutdown/
You can find out the UUID of your device with:
$ ls -la /dev/disk/by-uuid/ 

This will give you a list of your devices with symlinks pointing to their labels i.e /dev/sda1 etc. Alternatively you could run the following command, substituting /dev/sda1 to your device.
$ sudo blkid /dev/sda1

Finally to make autofs recognize your changes run:
$ /etc/init.d/autofs restart

Last but not least we have to make the script run at a certain interval. This is done via the cron daemon. To edit root's crontab type following, if you want to edit the current user's crontab just omit the "sudo":
$ sudo crontab -e

Add the following lines:

# Check if USB device with "systemHaltNow" is present
  * *   *   *   *    /PATH/ >/dev/null 2>&1

The stars tell cron to run the script once every minute forever and ever and ever. The last part stops cron from e-mailing you and logging everytime it executes the script, more info on that here.

Well that's it.