Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a way to run a script when a particular USB device is mounted?

I keep my videos on a separate USB and would like to run a script that would mount the video folder on the USB device to the one in the home folder.

share|improve this question
add comment

4 Answers 4

use udev

http://archive.atomicmpc.com.au/forums.asp?s=2&c=16&t=4775

share|improve this answer
3  
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. –  Stephen Myall Aug 20 '12 at 22:27
add comment

In Nautilus under Edit>Preferences>Media you can choose "other action" and than "costum command". for different kind of media to be executed. By that time the usb drive is already mounted, but I suppose you could still link it (with a costum command) to the folder you want the drive to appear in. I couldn't tell whether this is easier or better than using udev.

share|improve this answer
    
does the command wear a costume? or a costum? –  user84207 Sep 24 '13 at 15:11
add comment

Start by finding your device in lsusb. Note the ID (eg 0a81:0101)

Create a new udev rules file in /etc/udev/rules.d/ via sudoedit /etc/udev/rules.d/100-mount-videos.rulesand plonk a new rule in there like this:

ACTION=="add", ATTRS{idVendor}=="0a81", ATTRS{idProduct}=="0101", RUN+="/home/your_username/bin/mount_videos.sh"

Note how I used the ID from lsusb.

Then you just need to write the script to do the work. A simple mount command should work. You might need a sleep 5 command in there to wait for the filesystem to initialize (if you leave gnome to do the main mounting -- but you're free to mount it first and then you might not need the sleep).

Addition from Allan: Long running scripts might block "all further events for this or a dependent device". My Mint man page further states "Long running tasks need to be immediately detached from the event process itself." No tip is given on where to gain the skill to do this.

Reply from Oli: Wrap it like so: http://askubuntu.com/a/106359/449

share|improve this answer
3  
I might suggest prefixing the "RUN" command with "su your_user -c" so that the script is not running with root privileges. –  Kees Cook Feb 7 '11 at 6:29
2  
@Kees As the aim here is mounting, keeping root privs might be a good idea though. Perhaps instead of keeping the script in the user's home, you keep it in /root/ or somewhere where only root can edit it. –  Oli Feb 7 '11 at 11:11
    
This doesn't answer the question. The question asks how to run a script when a USB drive is mounted. This answer says how to do run a script that does mounts a USB drive when it is inserted. The difference matters to me because I have such a script (for a digital camera); without the script, the drive may or may be mounted on insertion (depending on user settings) and it's only when the drive is mounted that I want to do something (copy the images). So my script (which runs when the drive is added) often runs at the wrong time. –  Reinier Post Sep 15 '13 at 20:30
    
PS see ubuntuforums.org/showthread.php?t=1551835 –  Reinier Post Sep 15 '13 at 20:39
add comment

Another way to get the values for ATTRS{idVendor} and ATTRS{idProduct} (tested in Ubuntu 12.04) is:

  1. Find where your usb is mounted:

    $ mount | grep /dev/sd*
    

    this shows something like the following:

    /dev/sdb on /media/SOMEDIR type vfat ...
    
  2. Use udevadm to get that device info:

    udevadm info -q all -n /dev/sdb | grep -E -i -w '.*VENDOR_ID.*|.*MODEL_ID.*'
    

    the output should be something like:

    E: ID_MODEL_ID=001a
    E: ID_VENDOR_ID=002b
    
  3. Now use the model id for ATTRS{idProduct} and vendor id for ATTRS{idVendor}

    ACTION=="add", ATTRS{idVendor}=="002b", ATTRS{idProduct}=="001a" ...
    
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.