Udev rule is not being used?

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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'"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x