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!
Asked
Active
Viewed 82 times
1
-
Please see the answers to a similar question: http://askubuntu.com/questions/192331/how-to-get-an-email-notification-when-a-usb-storage-device-is-inserted/ The most user-friendly in my opinion is to go with Cuttlefish. – hytromo Apr 28 '14 at 15:45
-
Hmmm... don't. If you're running Linux from an USB disk, and this disk can be disconnected at any time, you can have all sort of problems. Crashes, corrupted files, whatever. (The answer below is ok for taking actions if some other USB device go off, but not if your "/" partition disappears...) – Rmano Apr 28 '14 at 16:18
1 Answers
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

Adrien Horgnies
- 285