-   Linux - Newbie (
-   -   Shutdown script possible to alert about connected USB devices? (

Roy Prins 07-21-2008 04:31 PM

Shutdown script possible to alert about connected USB devices?
When I shutdown, I would like to be alerted if any USB device is still plugged into my system. For comparison: on windows I use:

Does anything like it exist or can anyone point me to how this can be accomplished?

It should follow the following very simple logic:
  1. On shutdown: run the script.
  2. Check for USB devices (bluetooth, memory stick).
  3. If USB device found: display alert, nothing further.
  4. Else continue shutdown unimpeded.

GamerX 07-21-2008 04:40 PM

You'll need to use the "lsusb" command:

lsusb | grep "Memory Stick Identifier String"
Or you can do something more clever with awk instead of grep.

Roy Prins 07-21-2008 04:53 PM

Ok, I am getting USB-stick and bluetooth with the following:

lsusb | grep "Device 002"
Fine for now, should probably be refined more...

Now I need to hook it into the shutdown sequence somehow if it displays a non-empty list. Any hints for me? I am an utter newby...

This the way to go?

# update-rc.d -f start 90 0 6
And placing in /etc/init.d

FranDango 07-21-2008 05:16 PM

shutdown -k

/sbin/shutdown -k "warning-message: remove USB devices"
will issue a warning, but due to the '-k' option the system will not be shutdown. After having been reminded that way you can do the really shutdown.

Making it part of some shell script would allow to wait for some confirmation and then call the real shutdown sequence.

Roy Prins 07-21-2008 05:47 PM

That would be the "dumb" way of doing it, right? Knowing myself, I will hit return without thinking after 5 times and still forget my stick.

Not what I want. There should be a place for the less organized computer users as well.

kenoshi 07-22-2008 05:24 PM

This all depends on how you you:

1. use shutdown from gnome system menu?
2. use the shutdown command from a terminal?

If you are using shutdown from xterm, you can do something like the following example in your .bashrc:

shutdown() {

lsusb -d <vendor ID of your device> > /dev/null 2>&1

if [ $? = 0 ]; then

    echo "USB Device found, please make sure you unplug your usb drive!"


    echo "Shutting down you want to:"
    echo -e "\t1) shutdown"
    echo -e "\t2) poweroff"
    echo -e "\t3) reboot"
    echo -n "Enter choice and press [enter]:"
    read schoice

    case "$schoice" in
        1)      /sbin/shutdown now;;
        2)      /sbin/poweroff;;
        3)      /sbin/reboot;;
        *)      echo "invalid choice, shutdown cancelled";;



If you use gnome shutdown, there isn't a way I know of to script the shutdown sequence, most customization you can do is post session.

You can however do this:

1. disable shutdown in gnome

2. map hotkey to a shutdown script. Fire up gconf-editor (you may have to install "yum gconf-editor" if its not installed), browse in /apps/metacity/global_keybindings, change the value of any run_command not used (e.g. run_command_1) and map for example, <shift>F1 to it.

3. map command to hotkey. Create a script called /root/bin/, and in gconf-editor, goto /apps/metacity/keybinding_commands, and map say, command_1, to /root/bin/

Here's an example of what you could put into



sclist="True shutdown False poweroff False reboot"
lsusb -d <vendorID> > /dev/null 2>&1

if [ "$?" = "0" ]; then

    zenity --info --text "USB device found\!\n\nShutdown Aborted."


    schoice=$(zenity --title "Shutdown Options" --text \
    "Choose shutdown type, click cancel to abort." --list --radiolist \
    --column "Selection" --column "Shutdown Type $sclist)

    case "$schoice" in
        "")              zenity --info --text "Shutdown Aborted\!";;
        shutdown)        /sbin/shutdown now;;
        *)                /sbin/$schoice;;


So when you hit shift-f1 to shut down, this script will execute. This is just off the top of my head, I'm sure this could be optimized further, or someone knows of a better way to do this...hope this helps.

Roy Prins 07-23-2008 06:47 AM

Thx Kenosi,

From what I found out, there might not be an easy way to accomplish this. This looks as good as can be then.

I am a KDE user, but I guess the only difference would be in disabling shutdown.

All times are GMT -5. The time now is 10:04 AM.