X11 monitor usb storage

Please note that this blog has been moved.

Now it has its own domain: mynixworld.info 🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

I am using Gentoo with XFce desktop environment. I would like to get notified each time a new USB storage is attached/removed. I am sure that there exists applets/plugins that do that but I would like it to keep it simple. What could be simpler than a bash script that you can write by your self?

So I came up with the following solution:

  • write a simple bash script (eg: monitor_usb-storage) which will check every 5 seconds if a new scsi/usv-storage have been attached and was registered in the /proc, and if it does then a notify balloon will be shown. The same thing when the device is removed.
  • set up that script to be automatically launched at session start-up

How my script looks like:

declare -a usb_storage
declare -a usb_storage_desc
declare -a usb_storage_dev

function get_desc()
/bin/cat /proc/scsi/scsi |/usr/bin/awk -v id="scsi$1" '{i=index($0,id); if (i>0) {getline t; print t}}'

function get_dev()
if [ -d $dir ];then
/bin/ls $dir

function init()
if [ -d $PATH ];then
for d in $(/bin/ls $PATH);do
usb_storage=("${usb_storage[@]}" "$d")
info=$(get_desc $d)
usb_storage_desc=("${usb_storage_desc[@]}" "$info")
dev=$(get_dev $d)
usb_storage_dev=("${usb_storage_dev[@]}" "$dev")

function item_exists()
for item in "${usb_storage[@]}";do
if [ "$item" = "$1" ];then
echo $result

# initialize the usb_storage array with all existent usb-storage

while :;do
# scan /proc/scsi/usb-storage for newly attached USB storage devices
if [ -d $PATH ];then
for d in $(/bin/ls $PATH);do
exists=$(item_exists $d)
if [ "$exists" = "0" ];then
info=$(/bin/cat $PATH/$d|/usr/bin/awk '{sub(/^[ \t]+/,"");print}')
desc=$(get_desc $d)
dev=$(get_dev $d)
usb_storage=("${usb_storage[@]}" "$d")
usb_storage_desc=("${usb_storage_desc[@]}" "$desc")
usb_storage_dev=("${usb_storage_dev[@]}" "$dev")
/usr/bin/notify-send -i "/usr/share/icons/Tango/48x48/devices/usbpendrive_unmount.png" "USB storage attached" "$info"

let i=0

# check usb_storage array if contains items which does not exists any more in /proc/scsi/usb-storage (aka USB removed)
if [ ${#usb_storage[@]} -gt 0 ];then # if array not empty
while [ ${#usb_storage[@]} -gt 0 -a $i -lt  ${#usb_storage[@]} ];do # loop from 0 to end of array
if [ ! -e $PATH/$item ];then # if array item not exist on PATH
/usr/bin/notify-send -i "/usr/share/icons/Tango/48x48/devices/usbpendrive_unmount.png" "USB storage removed (${usb_storage_dev[$i]})" "${usb_storage_desc[$i]}"
unset usb_storage[$i]
unset usb_storage_desc[$i]
unset usb_storage_dev[$i]
let i=$i+1

# sleep about 5 seconds (we don't want to scan too agressive)
/usr/bin/sleep $FREQ

Start monitor_usb-storage as daemon at session start-up

In Xfce you can set up a Application Autostart (which are supposed to start at session start up):

  • Settings -> Session and Startup -> Application Autostart tab
  • define a new application autostart as bellow:
    • name: monitor_usb-storage (in fact it doesn’t matter how you will name it)
    • command: monitor_usb-storage

So, the daemon will start when your X session starts and then will monitor the new usb-storage attached/removed every 5 seconds. When new device attached/removed then a balloon notification like the one bellow will be shown:


About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux, X11 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s