Linux (Ubuntu) run script upon USB mount

Solution 1:

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.

Solution 2:

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