This question already has an answer here:

Ok here is what I am trying to accomplish,

I want to configure mutliple systems to have a script start when ever any usb stick is mounted and then if a specific folder is on the usb stick copy its contents to a folder on the system.

I have done research and found ways to run a script when a particular usb is mounted, but not any. secondly because I want the script to act on the newly mounted usb stick automatically, i would need its mount point, or path to root be passed to the script or some other workaround.

I actually already have a script made up to look for the dir, and then copy the directorys i want all done up but I dont know how to use it on any usb that is mounted

essentially the practical reason for this is that I want to be able to insert anystick into mutiple systems and automatically copy the files onto each system. I have 2 ubuntu installs and 2 others that are based on libreelec for raspberry pi so not many options to install extra packages, mostly basic systemd and bash scripting I hope

I want a cross linux solution as this will be used with both ubuntu and libreelec. I would prefer to use system d but udev may end up being a better solution.

marked as duplicate by Romeo Ninov, Jeff Schaller, Drav Sloan, DopeGhoti, GAD3R Jan 19 '18 at 20:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I can’t tell — do you want to configure your system(s) so they run a script whenever any USB stick is inserted, or do you want to create a USB drive that will force systems to run a script?  And what have you learned from your research?  (You have searched for an answer, right?  I suspect that this question has come up before.)  Please do not respond in comments; edit your question to make it clearer and more complete. – G-Man Jan 19 '18 at 1:51
  • I would like to configure multiple systems (thus some cross distro thinking is required) to mount any usb drive and then if a specific folder is resent on any usb drive, copy its contents to another location – Jordan Ketterer Jan 19 '18 at 2:27

A udev rule is the simplest way to accomplish this. udev rules are stored under /etc/udev/rules.d and usually follow the format of NN-filename.rule where NN determines which rule file is loaded first.

For example in /etc/udev/rules.d/10-local.rules adding the following will trigger an event every time a removable block device is attached with a kernel name in the format of sdX:

KERNEL=="sd?", SUBSYSTEM=="block", ATTR{removable}=="1", RUN+="/path/to/script.sh"

Note that this may launch your script before the device is mounted meaning you might have to mount your device manually.

If you want to match against different rules you can find out the attributes of all the events the device you are working with caused with udevadm info -a /dev/devname.

  • That ounds good, but one issue i still dont dont know how to overcome is passing the mount point to the script. see becasue the script needs to act on any usb stick the mount point will be a varible that i dont know how to get – Jordan Ketterer Jan 19 '18 at 15:44
  • Just pass the KERNEL variable to your script, see man udev under RUN for details. This might also be useful, stackoverflow.com/questions/13699241/… – imbuedHope Jan 19 '18 at 16:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.