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
Monitor usb storage - a working solution
How my script looks like:
#!/bin/bash
FREQ=5
PATH=/proc/scsi/usb-storage
ICON_RES="scalable"
ICON_EXT=".svg"
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()
{
dir=/sys/bus/scsi/devices/$1:0:0:0/block
if [ -d $dir ];then
/bin/ls $dir
fi
}
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")
done
fi
}
function item_exists()
{
result=0
for item in "${usb_storage[@]}";do
if [ "$item" = "$1" ];then
result=1
break
fi
done
echo $result
}
# initialize the usb_storage array with all existent usb-storage
init
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/${ICON_RES}/devices/usbpendrive_unmount${ICON_EXT}" "USB storage attached" "$info"
fi
done
fi
let i=0
# check usb_storage array if contains items which does not exists any more in /proc/scsi/usb-storage (aka USB removed)
while [ ${#usb_storage[@]} -gt 0 -a $i -lt ${#usb_storage[@]} ];do # loop from 0 to end of array
item=${usb_storage[$i]}
if [ ! -e $PATH/$item ];then # if array item not exist on PATH
/usr/bin/notify-send -i "/usr/share/icons/Tango/${ICON_RES}/devices/usbpendrive_unmount${ICON_EXT}" "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]
else
let i=$i+1
fi
done
# sleep about 5 seconds (we don't want to scan too agressive)
/usr/bin/sleep $FREQ
done
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:
Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.
Eugen Mihailescu
Latest posts by Eugen Mihailescu (see all)
- Dual monitor setup in Xfce - January 9, 2019
- Gentoo AMD Ryzen stabilizator - April 29, 2018
- Symfony Compile Error Failed opening required Proxies - January 22, 2018


