Saturday, June 13, 2015

Using udev rules to run a script on USB insertion

Question:
I'm trying to set up a script to run every time I plug in a USB device. I created the file /etc/udev/rules.d/90-local.rules and added the following rule:
ACTION=="add", SUBSYSTEM=="usb", KERNEL=="sd*", SYSFS{model}=="Cruzer*", RUN+="sh /home/jesse/Documents/Scripts/cruzer.sh"
The problem is that when the drive is connected, nothing happens. The script, for debugging purposes, is rigged to send a notification with notify-send, which is installed and works fine from the terminal.
The path to the script is correct, as I've run that exact command in the terminal without any troubles.

Answers:

I had the same problem. This worked for me:
Try to copy your script into /usr/local/bin and change the directory in your .rules file.
Also, I don't know what SYSFS is, but I would prefer to use the ATTR properties.
The following line is the content of my .rules file:
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]1", ATTRS{vendor}=="SanDisk ", RUN+="/usr/local/bin/backup.sh"
 
 
You can try to match the device by vendor and product IDs instead. The following custom rule works for me:
ACTION=="add", SUBSYSTEM=="usb", ATTR{idVendor}=="0763", ATTR{idProduct}=="019b", RUN+="/usr/bin/aconnect 20 128"
 

No comments:

Post a Comment