I am working on Raspian OS (Linux) for Raspberry Pi 3. I am trying to do a simple task say when I connect any USB to the port it should copy the data from a particular location to the USB and clear the file in that location, Here is my script(testscript.sh),

#gpio 1 and 0 are for led on/off while copying 
gpio -g mode 17 out
gpio -g write 17 1
sleep 10
sudo cp -f /home/pi/NewLog.log /media/usb/NewLog-$(date -u'+%Y%m%d%H%M').log
if [ $? -eq 0 ]; then
   echo -n > /home/pi/NewLog.log
gpio -g write 17 0
echo "content copied to USB"

I have created an UDEV rule in /etc/udev/rules.d

KERNEL=="sd*", SUBSYSTEMS=="usb", ACTION=="add",RUN+="/home/pi/testscript.sh"

I have also installed usbmount, so that the USB automounts in /media/usb folder I also found usbmount.rules in /lib/udev/rules.d

Now everytime I plug the USB it copies an Empty file to the USB, and after unplugging the USB the actual copied file is in location /media/usb...

I am guessing that the rule in /etc/udev/rules.d/myrule executes before the /lib/udev/rules.d/usbmount, maybe thats is why it copies the data to /media/usb before my USB mounts ! Any help will be much appreciated

1 Answer 1


You can probably check that the directory is a mountpoint (as per this answer) before copying anything. I would put the check in an infinite while loop, but I don't think this is very orthodox, since it could hang.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .