Beep on usb hotplug for laptop

Two months ago, I thought I discovered a cool trick in NSLU2-Linux wiki for beeping the PC’s internal speaker whenever a usb device is hotplugged. After poking around Ubuntu’s internals and some scripting, I created a howto for using the same trick in a Ubuntu PC. Finally, I proudly posted the howto in Ubuntuforums.

And there it sat, gathering dust. It did not appear that anybody has read the howto or find it useful.

I later tried to use the same script on a Dell laptop. While it worked, the beep sound was horribly loud and I couldn’t find a way to turn down the volume. Anyone who has installed Ubuntu in a Dell laptop would know the first thing you do is turn off system beep.

I started looking for a fix, and finally made use of the soundcard speakers, which was more controllable. Instead of using “beep” command, you use “aplay” command to play a wav sound. Here is the updated script.

#!/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

	# uncomment this part if using beep command
#	BEEP="/usr/bin/beep"
#	BEEPUP="$BEEP -l 100 -f 2000 -n -l 150 -f 3000"
#	BEEPDN="$BEEP -l 100 -f 3000 -n -l 150 -f 2000"

	# uncomment this part if using aplay command
	APLAY="/usr/bin/aplay"
	BEEPUP="$APLAY /usr/share/sounds/beepup.wav"
	BEEPDN="$APLAY /usr/share/sounds/beepdn.wav"

	STATFILE="/tmp/whatisinusb"
	CURR=`lsusb`
	PREV=`cat $STATFILE`

	if [ "${#CURR}" -gt "${#PREV}" ] ; then
		$BEEPUP
	fi

	if [ "${#CURR}" -lt "${#PREV}" ] ; then
		$BEEPDN
	fi

	if [ "$CURR" != "$PREV" ] ; then
		echo "$CURR" > $STATFILE
	fi

	rm -f "$LOCKFILE"
	trap - INT TERM EXIT
fi

Follow the Ubuntuforums howto page to add the script into your Ubuntu installation, but skip the parts related to the “beep” command. Instead, download beepdn.wav and beepup.wav sound files, and copy them to /usr/share/sounds/ directory.

sudo cp -v beep* /usr/share/sounds/

When I recently got an eeePC, I found that the “beep” command did not work with the eeePC. However, this new method of using “aplay” command fixed it nicely.

Post a Comment

Your email is never published nor shared. Required fields are marked *