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
Code: Select all
#!/bin/bash
echo "USB device removed at $(date)" >> /tmp/scripts.log
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"
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.
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.