Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a Ubuntu server with a external USB hard drive.

I basically just want to run a script whenever the hard drive is plugged in.

What would be the optimal way to achieve this ?

share|improve this question
    
You may want to ask this on superuser.com instead. –  Josh Oct 14 '09 at 22:50
    
Is this on a server, or a system with a GUI installed? –  Zoredache Oct 14 '09 at 23:19
    
Server (killing the retarded 15 char minimum by comment) –  h3. Oct 15 '09 at 2:31

2 Answers 2

up vote 11 down vote accepted

You can add a udev rule for your specific device -- to do this, create a file called /etc/udev/010custom.rules (or something similar; just make sure numerically it is the smallest in the directory). The files contents will be:

BUS="usb", SYSFS{idVendor}="**IDVENDOR**", SYSFS{product}="**PRODUCT**", NAME="usb/%k", SYMLINK="DEVICE"
RUN+="/path/to/your/script"

Replace the bolded keywords with the values for your device from lsusb:

Bus 005 Device 002: ID 0b05:b700 ASUSTek Computer, Inc. Broadcom Bluetooth 2.1

The first bold field above is IDVENDOR, and the second bold field is PRODUCT.

As for DEVICE, you can define this yourself; it will create a /dev/ node which is a symlink to any device that fits the criteria above (so, if you put foobar as the SYMLINK, udev will create a /dev/foobar which is a symlink to your USB device).

Note: I haven't tried this myself as I don't have any removable USB devices, but it should work. If you have any trouble check the udev documentation for rules.

share|improve this answer
    
I apologize if my writing is hard to follow; I'm a programmer, not a linguist. –  Michael Pobega Oct 15 '09 at 0:45
    
Thanks a lot, this should at least put me on the right path ;) –  h3. Oct 15 '09 at 2:33
    
You may want to note that RUN is meant to be on a newline, separate from BUS-SYMLINK -- ServerFault is just kind of weird with how it handles newlines, so I added a code tag. –  Michael Pobega Oct 15 '09 at 3:29
    
Oh.. I just added it at the end with a coma and it worked fine. However the script is called as expected .. but more than once. It's called like 15 times. I use the product and the device serial to match the device so I don't understand why. –  h3. Oct 15 '09 at 12:50
    
That's a bit weird. Why not have the script create some sort of lock file in /tmp telling it not to run more than once? So, something like 'if file /tmp/usb-lock exists exit, else run script' –  Michael Pobega Oct 15 '09 at 17:14

It is likely that you'll want to look at udev. Unfortunately, you've just witnessed me exhaust my knowledge of it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.