Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 07-21-2008, 05:31 PM   #1
Roy Prins
LQ Newbie
Registered: Jul 2008
Posts: 4

Rep: Reputation: 0
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.
Old 07-21-2008, 05:40 PM   #2
Registered: Nov 2006
Location: Vancouver, BC
Distribution: Slackware
Posts: 102

Rep: Reputation: Disabled
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.
Old 07-21-2008, 05:53 PM   #3
Roy Prins
LQ Newbie
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
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

Last edited by Roy Prins; 07-21-2008 at 05:59 PM. Reason: typoupdate
Old 07-21-2008, 06:16 PM   #4
Registered: Jun 2008
Posts: 101

Rep: Reputation: 15
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.
Old 07-21-2008, 06:47 PM   #5
Roy Prins
LQ Newbie
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
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.
Old 07-22-2008, 06:24 PM   #6
Registered: Sep 2007
Location: SF Bay Area, CA
Distribution: CentOS, SLES 10+, RHEL 3+, Debian Sarge
Posts: 159

Rep: Reputation: 32
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.
Old 07-23-2008, 07:47 AM   #7
Roy Prins
LQ Newbie
Registered: Jul 2008
Posts: 4

Original Poster
Rep: Reputation: 0
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.


script, shutdown, usb

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to detect usb devices connected to the system vijaya_svk Linux - Software 5 03-16-2008 03:21 PM
How to power off usb devices at shutdown? extrasolar Linux - Hardware 7 10-10-2007 12:19 AM
Slack 11 : Doesn't detect USB devices & can't shutdown ?? mnemonix Slackware 7 12-17-2006 07:04 AM
USB devices dont seem to be connected No1Daemon Linux - Hardware 5 03-22-2004 08:41 PM
PC hangs on startup with USB devices connected davee Linux - Hardware 5 09-12-2003 12:52 PM > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration