Page 1 of 5 123 ... LastLast
Results 1 to 10 of 43

Thread: howto run a script when a USB device is pluged in

  1. #1
    Join Date
    Oct 2006
    Beans
    149

    howto run a script when a USB device is pluged in

    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

    2. After doing this you know that
    - the vendor ID of your device is 040a
    - the product ID of your device is 0576

    3. Now is time to create your UDEV rule:
    Code:
    sudo nano /etc/udev/rules.d/85-my_rule.rules
    4. And add the text
    Code:
    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
    Last edited by Carlos Santiago; July 18th, 2007 at 02:49 PM.

  2. #2
    Join Date
    Jun 2005
    Location
    France
    Beans
    7,100
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: howto run a script when a USB device is pluged in

    Excelent guide thanks for sharing, will test and spread this, many of my friends were looking for such a thing, it is really usefull to automount you external drives with the good rights and especially if it's a NTFS drives and that you want to mount it with write support.

  3. #3
    Join Date
    May 2006
    Location
    Triangle, West Yorks, UK
    Beans
    978

    Re: howto run a script when a USB device is pluged in

    No offence to your excellent guide, but there is a much easier way. Simply make an executable script called autorun in the root drive of your USB flash drive. Then in the "Removable Drives and Media" preferences, make sure you tick the entry to automatically run scripts on plugging them in (sorry I am at work and can't see the exact wording, but it should be obvious which one it is). You're done! I've always wanted to do this, but frankly couldn't be bothered with poking around with udev; this is Ubuntu after all
    "The superior man understands what is right; the inferior man understands what will sell"
    --Confucius

  4. #4
    Join Date
    Oct 2006
    Beans
    149

    Re: howto run a script when a USB device is pluged in

    Reclusivemonkey, seems to me that you didn't notice one small, however very important, thing!
    This procedure is generic and works with ANY usb device. For example I use it to configure and start my internet connection when I plug in my UMTS modem.

  5. #5
    Join Date
    May 2006
    Location
    Triangle, West Yorks, UK
    Beans
    978

    Re: howto run a script when a USB device is pluged in

    Quote Originally Posted by Carlos Santiago View Post
    Reclusivemonkey, seems to me that you didn't notice one small, however very important, thing!
    This procedure is generic and works with ANY usb device. For example I use it to configure and start my internet connection when I plug in my UMTS modem.
    That's very true, please accept my apologies.

    But don't you think people will be interested in the USB flash way as well? I am sure plenty of people would like a quick and easy way of running scripts when they plug in their USB drives; especially for syncing.
    "The superior man understands what is right; the inferior man understands what will sell"
    --Confucius

  6. #6
    Join Date
    Oct 2006
    Beans
    149

    Re: howto run a script when a USB device is pluged in

    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.
    Code:
    ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", GROUP="dialout"
    2. 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.
    Code:
    ACTION=="add", SUBSYSTEM=="usb_device", SYSFS{idVendor}=="040a", SYSFS{idProduct}=="0576", SYMLINK+="ladys_camera"
    3. 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
    Code:
    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)
    Code:
    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
    Last edited by Carlos Santiago; July 17th, 2007 at 04:40 PM.

  7. #7
    Join Date
    Oct 2006
    Beans
    149

    Re: howto run a script when a USB device is pluged in

    There are several ways to skin a cat ... this is just one of them
    .... apologies accepted
    Last edited by Carlos Santiago; July 19th, 2007 at 01:33 PM.

  8. #8
    Join Date
    Aug 2007
    Beans
    46
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: howto run a script when a USB device is pluged in

    Sorry bout rezzing an older thread, but I'm running into issues.

    Ubuntu 7.10 Server

    1. I've created the udev rule in the proper directory (/etc/udev/rules.d/85-my_test.rules), and changed the idVendor, idProduct, and RUN info to reflect my device and script. When I plug in the device, the script never fires. Any ideas? If I run the script manually, it works great. Also, is the script run as root by udev?

    2. I would actually prefer reclusivemonkeys solution, but 7.10 Server doesn't seem to automount usb devices nor have the 'Removable Drives and Media GUI' for enabling autorun on external drives. Are there CLI methods of enabling these features?

    Thanks for any help,

    Edit:

    Ok, udev solution fixed now. Had to change SUBSYSTEM=="usb_device" to SUBSYSTEMS=="usb" and SYSFS to ATTRS.

    On the other solution, both automount & autorun are being handled by 'gnome-volume-manager' I think. Since this is Server, gnome isn't installed. I'm going to see if I can find a 'volume-manager'. Or maybe I could still install the g-v-m on Server...
    Last edited by Zylar; December 11th, 2007 at 09:25 PM.

  9. #9
    Join Date
    Dec 2007
    Beans
    9

    Re: howto run a script when a USB device is pluged in

    Carlos , thank you very much for your post.

    This will most certainly help me for building headless systems ,which need an automated backup system ,amongst many other usb plugin items.

    I didn't try it yet ,but I'm a big fan of non-"gui restricted" solutions.

    They tend to work on any version,and can easely be troubleshooted and customized.

    Many thanks again ,

    K.
    Belgium ...

  10. #10
    Join Date
    Oct 2006
    Beans
    149

    Re: howto run a script when a USB device is pluged in

    You welcome K.
    Note that the comparators must all be TRUE to execute the script.
    As Zylar notice the SUBSYSTEM may need to be changed to "usb". You could just remove the SUBSYSTEM comparator. If idVendor and idProduct match, you dont need to verify if it is a USB device.

    Also, if SYSFS does not work, try changing to ATTRS.

    Like:
    Code:
    ACTION=="add", ATTRS{idVendor}=="040a", ATTRS{idProduct}=="0576", RUN+="/usr/local/my_script"
    Regards,

    CS

Page 1 of 5 123 ... LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •