I answered a similar question with an example a while ago:
Running the script when it detects I/O
Use this command,
udevadm monitor --property
Insert the USB and check a property that is common to the devices that you want to use, e.g:
ID_TYPE=disk
ID_USB_DRIVER=usb-storage
Then create your rule
sudo gedit /etc/udev/rules.d/96-myusb.rules
with those values in
ACTION=="add", SUBSYSTEM=="usb",ENV{ID_TYPE}=="disk", ENV{ID_USB_DRIVER}=="usb-storage",RUN+="/usr/local/bin/myusb-add.sh"
ACTION=="remove", SUBSYSTEM=="usb",ENV{ID_TYPE}=="disk",ENV{ID_USB_DRIVER}=="usb-storage",RUN+="/usr/local/bin/myusb-remove.sh"
The rules file will only affect those devices that match the ENV{ID_TYPE}
and ENV{ID_USB_DRIVER}
. It will not affect any other device. If you want to restrict the rest of devices, maybe you can create a rule that matches the rest of devices and does nothing in the RUN
.
If you want to run the action only when a certain usb device is connected to an specific USB port, try adding ENV{DEVPATH}="yourUSBPATH" to the rules file. Substitute yourUSBPATH for the path that udevadm monitor --property
reports when you connect the device to the port that you want to use. I haven't tested this but I think it's a logical approach.
[udev][usb]
for all topic tagged withudev
&usb
. – user.dz Sep 17 '17 at 09:33