Linux – Cannot run script using udev rules

linuxudev

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

  1. udev does not produce output to any sort of terminal/notification. I found it here!

udev does not run these programs on any active terminal, and it does not execute them under the context of a shell. Be sure to ensure your program is marked executable, if it is a shell script ensure it starts with an appropriate shebang (e.g. #!/bin/sh), and do not expect any standard output to appear on your terminal.

  1. for redirecting output to the file, I was using ~ instead of the whole path of the user's home directory. Changing it to absolute path did produce the output. For the record, I put my rule under 12-hf-usb.rules. The only problem I am facing is that the script is executed twice, even after using RUN=. 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 it

A 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 by udevadm. Below are some useful commands:

  • udevadm monitor --udev to view udev activity upon adding/removing hardware in real time
  • lsusb to see attached usb devices
  • udevadm info --attribute-walk --name /dev/sdc? to view hierarchical details of devices

Source: http://www.jpichon.net/blog/2011/12/debugging-udev-rules/

Related Question