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

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

Hybrid View

  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
    Dec 2008
    Beans
    22
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question 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.
    is this also working with a pcmcia-umts-card?
    Could you provide me with your internet-connection-script, please?
    Thank you!

  7. #7
    Join Date
    Apr 2006
    Beans
    11

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

    I am trying to follow this guide to run a automatic backup script when mounting my Android phone. Here is my udev rule:

    ACTION=="add", KERNEL=="sd?1", SYSFS{serial}=="040363260701200E", RUN+="/usr/local/bin/backupDroid.sh"

    It sort of works. It runs the script. But it waits for the script to finish running BEFORE it mounts the device, which of course is not really helpful. Ideas?

  8. #8
    Join Date
    Oct 2006
    Beans
    149

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

    wtstommy, like I said in post #6 add the SYMLINK to your rule. Something like
    Code:
    ACTION=="add", KERNEL=="sd?1", SYSFS{serial}=="040363260701200E", RUN+="/usr/local/bin/backupDroid.sh",SYMLINK+="android_phone"
    When you plug it, your device should be accessible in /dev/android_phone.

    Then you should be able to mount it with:
    Code:
    sudo mkdir /mnt/android
    sudo mount /dev/android_phone /mnt/android
    The directory that will be the mount point of your device (in this case /mnt/android), you only need to create once.

    If you need to define the file system, try:
    Code:
    sudo mount -t vfat /dev/android_phone /mnt/android
    If the mount works, add the mount line in the beginning of your script.
    Last edited by Carlos Santiago; December 4th, 2009 at 06:47 PM.

  9. #9
    Join Date
    Apr 2006
    Beans
    11

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

    I found the answer to my question. You have to "detach" the executed script. I found out how to do that here: http://ubuntuforums.org/showthread.php?t=1098089

  10. #10
    Join Date
    Oct 2009
    Location
    Russian Federation, Ufa
    Beans
    19
    Distro
    Ubuntu 10.04 Lucid Lynx

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

    Just tried to write the same rule for my SD Card reader when I plug the correct SD card into it.

    Code:
    ACTION=="add", BUS=="pci", ATTRS{idVendor}=="fc30", ATTRS{idProduct}=="3da9", RUN+="/home/pechenie/.bin/photobackup.py"
    Why pci? Coz:

    Code:
    # lspci | grep SD
    0a:09.2 Mass storage controller: Texas Instruments 5-in-1 Multimedia Card Reader (SD/MMC/MS/MS PRO/xD)
    Also, idVendor:idProduct got from automouting folder:
    Code:
    /media/FC30-3DA9/
    And finally:
    Code:
    # fdisk -l
    Disk /dev/mmcblk0: 3999 MB, 3999268864 bytes
    82 heads, 17 sectors/track, 5603 cylinders
    Units = cylinders of 1394 * 512 = 713728 bytes
    Disk identifier: 0x00000000
    
            Device Boot      Start         End      Blocks   Id  System
    /dev/mmcblk0p1               6        5604     3901440    b  W95 FAT32
    Any suggestions?

Page 1 of 4 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
  •