20

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.

Flimzy
  • 4,465
JTeK
  • 313

6 Answers6

8

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"
ale
  • 3,410
plk
  • 96
7

notify-send requires access to your DBus session bus, which it cannot have for two reasons:

  • No information about the session. When started by udev, your script knows nothing about where you are logged in or if you're logged in at all. Multi-seat with X11 is still tricky, but user switching works for both X11 and console sessions. Many people also use SSH, VNC and NX over the network.

    (DISPLAY=:0 would work half of the time, but that's still guessing at best.)

  • Denied by DBus policy. Even if your script somehow finds your X11 session, it will not be able to send notifications due to the script being run as root instead of your user account.

grawity
  • 501,077
1

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"

You can see idVendor and idProduct in the lsusb output or dmesg after attaching the device.

Gareth
  • 19,080
Aleh
  • 121
1

Following up on Aleh's answer: If you also want to monitor remove events, you need to look for an environment variable named ID_SERIAL. It contains vendor and product ID separated by underscore:

ACTION=="add|remove", SUBSYSTEM=="usb", ENV{ID_SERIAL}=="0763_019b" RUN+="/usr/bin/aconnect 20 128"

The rule is now shorter, too.

krlmlr
  • 902
0

Perhaps, you may need to add a sleep within the script, to give the usb device a chance to "settle" down? For example, usb 3g modems, mode switching to get a /dev/ttyUSB to get kicked up and running by the kernel.

t0mm13b
  • 723
-1

Try to replace SUBSYSTEM="usb" with SUBSYSTEMS="usb"

arnaud
  • 1