2

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 Answer 1

3

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" 
Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work. I wonder that ENV{idVendor} is exists or not. But thank you anyway.
You are right! Use ID_VENDOR_ID instead of idVendor, and ID_MODEL_ID replace the idProduct, then it works. Thanks.
Please be aware! I was following a tutorial which did not mention that udev rules must be on one line! Your lines should be comma-separated on one line, as it will see each line not starting with # (a comment) as a new rule. This was very bad for me because I was detecting USB removal on my device and calling my lockscreen (physlock). The result was the eternal calling of my lockscreen at boot (and as I manually bring up my network interfaces, I had no method of remote login.) I ended up needing to boot from a live iso to delete the .rules file so I could log in!
@taigrr you split long lines with `ACTION=="remove", \` backslash

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.