How do I use udev to run a shell script when a USB device is removed?

2.4k views Asked by At

This is my rule:

ENV{DEVTYPE}=="usb_device", 
ACTION=="remove", 
SUBSYSTEM=="usb", 
ATTR{idVendor}=="8829", 
ATTR{idProduct}=="0010", 
RUN+="/bin/mkdir /home/zkd/123"

It doesn't work. But this rule:

ENV{DEVTYPE}=="usb_device", 
ACTION=="remove", 
SUBSYSTEM=="usb", 
RUN+="/bin/mkdir /home/zkd/123"

It works!! And this also works:

ENV{DEVTYPE}=="usb_device", 
ACTION=="add", 
SUBSYSTEM=="usb", 
ATTR{idVendor}=="8829", 
ATTR{idProduct}=="0010", 
RUN+="/bin/mkdir /home/zkd/123"

So how do I use udev to run a shell script when a USB device is removed? I must recognize the USB device with idProduct and idVendor. Thank for your answer!

1

There are 1 answers

4
Ignacio Vazquez-Abrams On BEST ANSWER

Information about removed devices is passed in ENV rather than in ATTR since there's no longer any device to extract information from.

ENV{DEVTYPE}=="usb_device", 
ACTION=="remove", 
SUBSYSTEM=="usb", 
ENV{idVendor}=="8829", 
ENV{idProduct}=="0010", 
RUN+="/bin/mkdir /home/zkd/123"