Add a udev rule to run a script when usb is un/plugged

Share your own howto's etc. Not for support questions!

Add a udev rule to run a script when usb is un/plugged

Postby Deb-fan » 2020-09-10 05:39

Okay, decided it was time to figure out how udev rules work, googled + adapted what I found a bit and here goes ...

In my case I have a .(dot) directory kept in my users /home directory named .bin, I stash my user scripts here, this is where I'm storing and made the following scripts.

Create two txt files, named one of them device_add.sh, the other device_removed.sh and then add the following contents. Begin your-any bash scripts with the proper shebang, this #!/bin/bash and I always named them with the .sh extension, it's good practice as regards scripting.

For device_add.sh this ...

Code: Select all
#!/bin/bash
echo "USB device added at $(date)" >> /tmp/scripts.log


For device_removed.sh this ...

Code: Select all
#!/bin/bash
echo "USB device removed at $(date)" >> /tmp/scripts.log


Save them, make them executable with ie: "chmod +x .bin/device_add.sh" and of course do the other as well. In this case what they are intended to do is log the time/date when I plug and unplug a usb thumb drive I'm using here. Next up let's plug in a usb drive and see what the thing is called ... Plug it in and run the "lsblk" cmd in a terminal, in my case turns out that's sdc and it's got two partitions on the puppy sdc1 and sdc2. Note: That the lsblk cmd lists out block devices attached to the system, storage devices like usb drives are block devices. Now I want some further info about the device so I'll use "udevadm info /dev/sdc" and it'll give me a bunch of such info, next up am going to create the file with the udev rule I want to use when this device is un/plugged. I created a file (do so as root/sudo) in the following and yeppers it's named 80-test.rules /etc/udev/rules.d/80-test.rules with the following contents ...

Code: Select all
SUBSYSTEM=="usb", ACTION=="add", ENV{ID_MODEL_ID}=="6545", RUN+="/home/myusername/.bin/device_add.sh"
SUBSYSTEM=="usb", ACTION=="remove", ENV{ID_MODEL_ID}=="6545", RUN+="/home/myusername/.bin/device_removed.sh"


Obviously the RUN+="/is/the/path2/the/scriptname.sh" there an interesting part is the ENV{} thing, I chose to use the ID_MODEL_ID, in my case that was 6545, which I found via that "udevadm info /dev/sdc" cmd above, to identify the device. Play around and try some others if wanted/needed to identify the usb device you wish to tailor a udev rule for. I might've went with ID_VENDOR, which in my case for that thumb drive is a Kingston. So I'd have used ENV{ID_VENDOR}=="Kingston" had I went with that.

We're going to just reboot at this point so the new rules file is added, though this is supposedly also done with "sudo udevadm control --reload" and it probably does, though didn't in my case, so I rebooted, then created the log file in /tmp "touch /tmp/scripts.log" in terminal after having rebooted, files in /tmp are temporary, okay so at this point, I've got things all setup, I've created the log file in /tmp and it's time to see the magic happen. :D

I plug my thumb drive into a usb port and then "cat /tmp/scripts.log" and !?!?!? TADA ... it shows me the time/date I plugged the thing in .... I unmount and remove the thumb drive, rerun the cmd to check, TADA AGAIN, shows me I just removed the thing, whooooohooooo. :) Plug it into another usb port, check again ... yep, it's been logged. This is pretty cool though and could prove useful. Personally considering using something similar as this to run a script to rsync files that I end up transferring from my systems hard drive to a usb drive often automagically for me.

Now use this info-tidbit to release your inner nix ninja's. If you figure out something cool and feel like sharing it and/or bragging about such, here is as good a place as any for it. :)
Most powerful FREE tech-support tool on the planet * HERE. *
Deb-fan
 
Posts: 968
Joined: 2012-08-14 12:27

Return to Docs, Howtos, Tips & Tricks

Who is online

Users browsing this forum: No registered users and 5 guests

fashionable