1

I am running Ubuntu 12.04 from a flash drive, and am in a situation where the USB drive it is running from (along with other USB devices) can be disconnected very easily (wonky/bent ports). What would be the easiest way to run a command (in this case, a script that terminates running servers and sounds an alarm) when any USB device, including the drive it is running from, disconnects? Thanks!

1 Answers1

1

You have to make an udev rules. To do so, you must create a file like this :

$ sudo vim /etc/udev/rules.d/.rules

The name can be whatever you want as long as it ends with .rules

Now here is how you write a rule (it's one I use) :

ACTION=="add", ENV{ID_FS_UUID}=="E040-9945", RUN+="/bin/su adrien -c 'export DISPLAY=:0 && /usr/bin/gnome-screensaver-command -d'"
ACTION=="remove", ENV{ID_FS_UUID}=="E040-9945", RUN+="/bin/su adrien -c 'export DISPLAY=:0 && /usr/bin/gnome-screensaver-command -l'"

First you must specify what conditions to trigger the rule. Here I use the action and the UUID of my usb key. Then I specify a script to run (This one lock/unlock my screen). (You can put as many RUN+="" as you need.)

In your case, don't put the UUID condition but rather something like DRIVERS=="usb-storage"

To find out what property to check, plug an usb key, let's say it goes to /dev/sdb and run :

$ udevadm info -a -p /sys/block/sdb | grep usb