Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the state-of-the-art method for automatically executing custom scripts upon USB device plug-in under current Linux distributions like Debian/CentOS/Fedora?

For example if you want to automatically mount/copy some files/umount a USB mass storage device based on its UUID (or device ID etc.).

share|improve this question
2  
Don't use udev that much, but check this out. Looks easy. –  Herman Torjussen Jan 7 '12 at 20:48

2 Answers 2

up vote 6 down vote accepted

Put a line like this in a file in /etc/udev/rules.d:

KERNEL=="sd*", ATTRS{vendor}=="Yoyodyne", ATTRS{model}=="XYZ42", ATTRS{serial}=="123465789", RUN+="/pathto/script"

Add a clause like NAME="subdir/mydisk%n" if you want to use a custom entry path under /dev.

Run udevadm -a -n sdb to see what attributes you can match against (attribute=="value").

share|improve this answer
    
Is this answer still valid? Under no amount of fiddling can I get the script to execute (and touch a file). If you can provide the most basic example that executes a script when anything is plugged in, that would be great and we can work from there. –  Redsandro Feb 22 '13 at 15:00
    
@Redsandro I have variations of this rule in my udev configuration and they're working. Did you plug the device out and back in, or run udevadm trigger? Does your system use udev? If you want to run a script when anything is plugged in, remove all the conditions (the clauses with ==) (maybe the syntax requires one condition, I'm not sure; if it does, use KERNEL=="*"). –  Gilles Feb 22 '13 at 15:28
    
I tried that too, but since it works for you, I must be missing a step. I have moved this question here, so I can write example code. –  Redsandro Feb 22 '13 at 15:36

I looked into /lib/udev/rules.d for examples of disk related rules. On an Ubuntu system one rule file provides the environment variable ID_FS_UUID_ENC which you can use in own rule files.

Thus I put a custom rule file under /etc/udev/rules.d/foodevice.rules. Since it is not prefixed with a number, it is ran at last by udev. Btw, the udev daemon watched /etc/udev/rules.d for changes such that you don't need to restart it on file changes.

The content of /etc/udev/rules.d/foodevice.rules is:

ACTION=="add", KERNEL=="sd*[!0-9]", ENV{ID_FS_UUID_ENC}=="FFFF-AAAF",
  RUN+="/usr/bin/sudo -u juser /home/juser/path/script.sh"

(this is one rule - you have to remove the newline after the ENV clause because udev does not have a line continuation mechanism)

A program started by udev blocks the daemon - thus it shouldn't run for a long time. I solved it via at - i.e. via detaching from the process doing the real work:

$ cat /home/juser/path/script.sh
#!/bin/sh
echo ~/path/mountcopystuff.sh | at now
share|improve this answer
    
You might need to reload the udev rules using udevadm control --reload-rules, depending on your distribution. –  user44038 Jul 29 '13 at 13:44
1  
Instead of at now I suggest batch. –  Martin Schröder Jun 23 at 16:04

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.