Scripts – Running Script When It Detects I/O

bashbluetoothcommand linemousescripts

I have bluetooth mouse xinput setting script to run whenever i have connect the mouse. Currently I'm simply alias the short key as 'bm' for that bash file execution but I want to know if there is automatic way to execute the bash file or alias command whenever it detects the mouse connection.

Thank in advance!

ubuntu 16.10

Best Answer

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.