I would use a .rules file.
First, find out the ID_VENDOR_ID
and the ID_MODEL_ID
of your mouse. Disconnect the mouse, run this command and connect the mouse (the |grep ID
part is only to filter information that you don't need).
udevadm monitor --property|grep ID
Lets say that you get these values:
ID_VENDOR_ID=0a12
ID_MODEL_ID=0001
Now create a file in the rules folder (96 is the priority of the rule):
sudo gedit /etc/udev/rules.d/96-myusb.rules
Add these two lines using your values for the ID_VENDOR_ID
and ID_MODEL_ID
. If you don't want to do anything when you remove it, don't include the second line.
ACTION=="add", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12", ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-add.sh"
ACTION=="remove", SUBSYSTEM=="usb",ENV{ID_VENDOR_ID}=="0a12",ENV{ID_MODEL_ID}=="0001",RUN+="/usr/local/bin/myusb-remove.sh"
You can test that it works creating the two scripts:
$ sudo gedit /usr/local/bin/myusb-add.sh
Add something like this (change add
to remove
in the other one):
#!/bin/bash
echo "added" >> /tmp/myusb.log
Finally, tail the file with tail -f /tmp/myusb.log
and connect/disconnect your mouse. You should see that the text gets added to the file.