I would like a shell script to execute each time I insert a USB modem.
How would I do this?
You can accomplish this with some udev rules, as @Rinzwind suggested.
First, save your script to somewhere accessible by other users. For example, you could save it as /usr/local/bin/script.sh
.
Now, make the script accessible by others.
sudo chmod 555 /usr/local/bin/script.sh
Now, plug in your USB modem and then run lsusb
. Below is an example output:
$ lsusb
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 008 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 007 Device 002: ID 0fcf:1008 Dynastream Innovations, Inc. Mini stick Suunto
Bus 007 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 006 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Find the line with your modem's name at the end. Now, find the portion near the middle in the format ID XXXX:####
. The XXXX
and ####
portions will be, the Vendor ID and Device ID respectively.
For this example for the third line, which reads … ID 0fcf:1008 Dynastream …
, the vendor ID is 0fcf
and the Device ID is 1008
. Your device will have different values for these IDs.
Now, we will use these IDs to create a rule for udev.
Now, create a rule for this device with udev.
Make a rules file with
sudo nano /etc/udev/rules.d/90-local.rules
Add the following line:
ACTION=="add", SUBSYSTEM=="usb", ATTRS{vendor}=="0xXXXX", ATTRS{device}=="0x####", RUN+="/usr/local/bin/script.sh"
Replace XXXX
with the vendor id from earlier, and replace ####
with the product id from earlier.
Now, force udev to reload it's rules (alternatively you could skip this step and reboot)
udevadm control --reload-rules
sudo service udev restart
And you're done!
Sources: Similar question on SU concerning udev rules, Creating udev Rules