Prescript: I have already searched for similar questions but am not able to find a useful answer.
Background: I am using Fedora 64-bit. I want to execute a specific script /bin/usbattached.sh when a usb is attached. I am using Kingston Datatraveller usb stick whose Vendor Id is 0951 and Product Id is 1660 (as found from dmesg and other methods). I have found two udev rules.d directories on my machine, namely /etc/udev/rules.d and /lib/udev/rules.d Since I am not sure which directory to use, I have created my rule file in both of above mentioned directories. Below is my rule
ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1660", RUN+="/bin/usbattached.sh"
I have removed ACTION, changed RUN+= to RUN=, written it in new file as well as pre-existing rule files, all without luck.
I use
udevadm control --reload-rules
and
systemctl restart udev.service
to restart udev.
When I run udevadm test on my device, it shows that it does read my rules file but the script doesn't work. Any method to debug it will be highly appreciated.
The script /bin/usbattached.sh sends a notification to the top-right corner of the screen, echo's a message on the screen and appends the current date/time to ~/op file, when run manually. Unfortunately, none of the above mentioned actions are being performed when the usb is attached.
EDIT: Upon adding ´sleep 10´ to my usbattached.sh script, I notice the usb is mounted roughly after 18 seconds. This time changes almost linearly with changing the value from 10 as well. Looks like the script IS being run but I am not having any notification, neither there is a new line appended to the output file
Best Answer
Just a couple of silly mistakes on my part. Below are the reasons for no output
12-hf-usb.rules
. The only problem I am facing is that the script is executed twice, even after usingRUN=
. I'll edit the answer once I find it. It looks like I have to make the rule more specific, to match only one device. It is not important for me at the moment so I will skip itA lot of people are facing trouble while using udev. Here is some help to go through the problems:
udevinfo
and related tools have been replaced byudevadm
. Below are some useful commands:udevadm monitor --udev
to view udev activity upon adding/removing hardware in real timelsusb
to see attached usb devicesudevadm info --attribute-walk --name /dev/sdc?
to view hierarchical details of devicesSource: http://www.jpichon.net/blog/2011/12/debugging-udev-rules/