A. Introduction
I came upon this page when playing around with a Linksys NSLU2. After understanding the script at the bottom of that page, it occurred to me that I could do something similar for an Ubuntu PC. Wouldn't it be nice for the PC speaker to beep when you plug/unplug a USB device? I went about modifying the script to make a simpler one for beeping the internal PC speaker in Ubuntu.
Note: I am referring to the internal PC speaker, not the audio output from the sound card.
B. Prerequisite
Tested OK in the following systems:
1. Ubuntu Intrepid 8.10 86_x64
2. Ubuntu Hardy 8.04 86_x64
3. Ubuntu Hardy 8.04 i386
4. Ubuntu Gutsy 7.10 i386
The following steps rely on the terminal. Open a terminal window by:
Panel menu > Application > Accessories > Terminal
Then, copy/paste the codes for each of the steps.
C. Install
1. Install beep
This will install the beep program, that will emit the sound from the PC speaker.
Code:
sudo apt-get install beep
.
2. Test beep
You should hear a beep from the PC speaker.
3. Create the script file
Code:
cd ~
cat > usb-hotplug.sh << "EOF"
#!/bin/bash
LOCKFILE="/tmp/usb-hotplug.lock"
# Lock to convert multiple triggers to single run
if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) 2> /dev/null; then
trap 'rm -f "$LOCKFILE"; exit $?' INT TERM EXIT
BEEP="/usr/bin/beep"
STATFILE="/tmp/whatisinusb"
CURR=`lsusb`
PREV=`cat $STATFILE`
if [ "${#CURR}" -gt "${#PREV}" ] ; then
$BEEP -l 100 -f 2000 -n -l 150 -f 3000
fi
if [ "${#CURR}" -lt "${#PREV}" ] ; then
$BEEP -l 100 -f 3000 -n -l 150 -f 2000
fi
if [ "$CURR" != "$PREV" ] ; then
echo "$CURR" > $STATFILE
fi
rm -f "$LOCKFILE"
trap - INT TERM EXIT
fi
EOF
.
4. Move the script to /bin
Code:
sudo cp ~/usb-hotplug.sh /bin/usb-hotplug.sh
.
5. Make the script executable
Code:
sudo chmod +x /bin/usb-hotplug.sh
.
6. Add udev rule to trigger the script upon an usb hotplug event
Code:
echo BUS==\"usb\", RUN+=\"/bin/usb-hotplug.sh\" | sudo tee -a /etc/udev/rules.d/80-programs.rules
.
C. End notes
That's it! It should work immediately, without any reboot.
D. Uninstall
If you find the beepings irritating after a while, you can remove it by:
1. Remove the udev rule
Fire-up a text editor with root privilege. If you are using Ubuntu/gnome, run the command below:
Code:
gksudo gedit /etc/udev/rules.d/80-programs.rules
Delete the line:
Code:
BUS=="usb", RUN+="/bin/usb-hotplug.sh"
.
2. Remove usb-hotplug.sh
Code:
sudo rm /bin/usb-hotplug.sh
.
3. Optional: remove beep
Code:
sudo apt-get remove beep
.
.
Bookmarks