6

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.

1 Answer 1

8

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 /bin/sh -c to run your command so it will process the redirection:

SUBSYSTEM=="block", ATTRS{model}=="2AS", ACTION=="add", RUN+="/bin/sh -c '/bin/echo inserted lacie >> /home/herman/udev_file'"
2
  • 1
    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.
    – user13742
    Commented Jun 9, 2012 at 20:24
  • /bin/sh <--absolute path seems like an essential component. Worked for me this way too in Arch Linux. Commented Apr 3, 2015 at 22:44

You must log in to answer this question.