Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Execute script on unplugging of usb-stick
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware
View previous topic :: View next topic  
Author Message
neonknight
Guru
Guru


Joined: 19 Jul 2003
Posts: 357
Location: Zurich, Switzerland

PostPosted: Tue Jan 24, 2006 9:18 pm    Post subject: Execute script on unplugging of usb-stick Reply with quote

I'd like to execute a script or command when I unplug my USB-memory-stick (flash drive or whatever you may call it). But I don't really know how to do this. Executing a script when plugging it in seems to be no problem.

I have tried it with a hotplug-rule:
Create a file /etc/hotplug/usb/usb-storage and added a few commands (for testing purpose: logger "hotplug script was executed"). Well, it works - when plugging the stick in. But there's no go when unplugging.

Second attempt was to write a udev-rule:
BUS="scsi", KERNEL="sd?", SYSFS(vendor)="3SYSTEM", SYSFS(model)="USB FLASH DISK", PROGRAM="/usr/bin/logger 'udev rule has executed command'"
Uhm, what to say. It works also only when plugging in.

Of course I could parse the syslog or check for the device's uuid every few seconds. But polling is not a nice realtime-response and uses too many ressources :)

What else could I do to execute the script when the stick has been removed?
Back to top
View user's profile Send private message
Headrush
Watchman
Watchman


Joined: 06 Nov 2003
Posts: 5597
Location: Bizarro World

PostPosted: Tue Jan 24, 2006 10:27 pm    Post subject: Reply with quote

You need to use usermap files in /etc/hotplug/. There are several files in there already that you can copy and adjust.

The usermap lists the device ID and the program to call when hotplug sees a change.

In the program you call, you can have different actions for ADD, REMOVE, etc, like this example for my scanner:
Code:
if [ -z "$DEVICE" ] ; then
  IF=`echo $DEVPATH | sed 's/\(bus\/usb\/devices\/\)\(.*\)-\(.*\)/\2/'`
  DEV=`echo $DEVPATH | sed 's/\(bus\/usb\/devices\/\)\(.*\)-\(.*\)/\3/'`
  DEV=`expr $DEV + 1`
  DEVICE=`printf '/proc/bus/usb/%.03d/%.03d' $IF $DEV`
fi

if [ -z "${DEVICE}" ] ; then
        IF=$(echo ${DEVPATH} | sed 's:\(bus/usb/devices/\)\(.*\)-\(.*\):\2:')
        if [ -r /sys/${DEVPATH}/devnum ]; then
             DEV=$(cat /sys/${DEVPATH}/devnum)
        else
             DEV=1 # you'll have to adjust this manually for kernel < 2.6.6
        fi
        DEVICE=$(printf '/proc/bus/usb/%.03d/%.03d' ${IF} ${DEV})
fi

if [ "$ACTION" = "add" -a "$TYPE" = "usb" ]; then
  chown root:scanner "$DEVICE"
  chmod 0660 "$DEVICE"
fi

This is a simple case and you can do whatever you need here.
Back to top
View user's profile Send private message
neonknight
Guru
Guru


Joined: 19 Jul 2003
Posts: 357
Location: Zurich, Switzerland

PostPosted: Thu Jan 26, 2006 5:33 pm    Post subject: Reply with quote

Thanks for your reply! I will try that.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Kernel & Hardware All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum