Skip to content

Instantly share code, notes, and snippets.

@tpowellcio
Last active May 24, 2016 14:35
Show Gist options
  • Save tpowellcio/28f2115f02838f6a9b9873a84cb973cc to your computer and use it in GitHub Desktop.
Save tpowellcio/28f2115f02838f6a9b9873a84cb973cc to your computer and use it in GitHub Desktop.
Automatically Clone Files to USB upon insert

#Automate USB Duplication Script on plug in ref:http://ubuntuforums.org/archive/index.php/t-502864.html

This explains how you could run a script made by you (say /usr/local/my_script) when you plug a specific USB device.

  1. First run lsusb to identify your device. Example:
$lsusb
Bus 004 Device 001: ID 0000:0000
Bus 003 Device 001: ID 040a:0576 Kodak Co.
Bus 002 Device 001: ID 0000:0000
Bus 001 Device 001: ID 0000:0000
  1. After doing this you know that
  • the vendor ID of your device is 040a
  • the product ID of your device is 0576
  1. Now is time to create your UDEV rule:
sudo nano /etc/udev/rules.d/85-my_rule.rules
  1. And add the text
ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", RUN+="/usr/local/my_script"

Explanation: When the usb_device product identified as 0576 of vendor 040a is added, run /usr/local/my_script Note that == and != are comparators, while = and += are assingments

#Other udev rules Due to the interest of this subject, I would like to emphasize that udev rules has other very useful features, for example:

  1. if you had GROUP="dialout", the device will belong to that group. This means that all users in the dialout group have special permissions to work with the device.
ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", GROUP="dialout"
  1. if you had SYMLINK+="ladys_camera, a ladys_camera device will be added to your /dev. This could be very usefull if you use several usb devices (as most of us do), because their device (/dev/ttyUSB[0-9]) is set depending on plug order. This way /dev/ladys_camera will always be your lady's camera.
ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", SYMLINK+="ladys_camera"
  1. Also notice that udev rules work with all king of devices: usb, serial, pcmcia, ... udev rules is a simple and easy way to configure all devices (it can configure, load modules, run scripts, set permissions for all your hardware).

Final notes:

A. Each line is a device. If you want to break a line add \ at the end

ACTION=="add", SUBSYSTEM=="usb_device", \
SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", \
SYMLINK+="ladys_camera"

B. You can add simultaneous actions in the same line (like group assignment and symbolic link)

ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", GROUP="dialout", SYMLINK+="my_modem"

C. The rules file must be in /etc/udev/rules.d/. Restart udev service (sudo /etc/init.d/udev restart) after changes.

D. The rules filename must start with two digits, plus a dash (-), and must end with .rules The two digits have a reason. See /etc/udev/rules.d/README

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment