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.

I'm setting up udev to handle my external USB hard drive when it gets plugged in and removed (no mounting yet). So far, I've made two new rules in /etc/udev/rules.d/10-local.rules to log adding/removing the disk to a file:

SUBSYSTEM=="block", ATTRS{model}=="2AS", ACTION=="add", RUN+="/bin/echo 'inserted lacie' >> /home/herman/udev_file"
SUBSYSTEM=="block", ATTRS{model}=="2AS", ACTION=="remove", RUN+="/bin/echo 'removed lacie' >> /home/herman/udev_file"

I have started the udevd daemon and re-plugged the hard drive several times, but /home/herman/udev_file is never created . In the rules I've used keys from the command udevadm info -a -n /dev/sdb which is the hard drive in question. What am I doing wrong here? The disk is being detected and I'm able to mount it just fine. Any help appreciated.

share|improve this question

1 Answer 1

up vote 6 down vote accepted

udev outputs logging information to /var/log/messages, but by default it only logs errors, and it happens you've constructed a command that doesn't do what you want, but also doesn't error out. The >> redirection is handled by your shell, and udev doesn't run the command through a shell, so it's literally running the binary /bin/echo and passing it the arguments 'inserted lacie' >> /home/herman/udev_file. If you change udev to log more (edit /etc/udev/udev.conf and add the line udev_log="info"), you'll see that it runs that command, and the output is 'inserted lacie' >> /home/herman/udev_file

Personally, I prefer making short shell scripts that do what I want, so I can edit them without restarting udev, but you can also use sh -c to run your command so it will process the redirection:

SUBSYSTEM=="block", ATTRS{model}=="2AS", ACTION=="add", RUN+="sh -c '/bin/echo inserted lacie >> /home/herman/udev_file'"
share|improve this answer
    
Thanks, I did replace the echo line with an executable script and it did work. Your example above worked for me when using absolute path to sh: RUN+="/bin/sh -c '..'". Now my problem is different - the "add" action is called several times - I can't seem to match the usb drive "uniquely" enough. –  Herman Torjussen Jun 9 '12 at 20:24

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.