LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

Reply
 
Search this Thread
Old 07-21-2008, 04: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:
4sysops.com/archives/never-forget-your-usb-stick-again

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, 04:40 PM   #2
GamerX
Member
 
Registered: Nov 2006
Location: Vancouver, BC
Distribution: Slackware
Posts: 102

Rep: Reputation: Disabled
You'll need to use the "lsusb" command:
Code:
lsusb | grep "Memory Stick Identifier String"
Or you can do something more clever with awk instead of grep.
 
Old 07-21-2008, 04: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:
Quote:
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?
Quote:
# update-rc.d -f foo.sh start 90 0 6
And placing foo.sh in /etc/init.d

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

Rep: Reputation: 15
shutdown -k

Running
Code:
/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, 05: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, 05:24 PM   #6
kenoshi
Member
 
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 shutdown...do 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:
Code:
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!"

else

    echo "Shutting down system...do 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";;
    esac

fi

}
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

https://www.redhat.com/archives/fedo.../msg01903.html

2. map hotkey to a shutdown script. Fire up gconf-editor (you may have to install it...run "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/sd.sh, and in gconf-editor, goto /apps/metacity/keybinding_commands, and map say, command_1, to /root/bin/sd.sh.

Here's an example of what you could put into sd.sh:

Code:
#!/bin/bash

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

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

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

else

    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;;
    esac

fi
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, 06: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.
 
  


Reply

Tags
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 02:21 PM
How to power off usb devices at shutdown? extrasolar Linux - Hardware 7 10-09-2007 11:19 PM
Slack 11 : Doesn't detect USB devices & can't shutdown ?? mnemonix Slackware 7 12-17-2006 06:04 AM
USB devices dont seem to be connected No1Daemon Linux - Hardware 5 03-22-2004 07:41 PM
PC hangs on startup with USB devices connected davee Linux - Hardware 5 09-12-2003 11:52 AM


All times are GMT -5. The time now is 07:12 PM.

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