Skip to content
Advertisement

Copy file to USB automatically on mount Raspberry Pi

I’m currently doing a project which requires a file to be automatically copied to USB-stick on mount. Based on my research in internet, I believe it is doable using udev rule. So, when an USB is inserted into my RPi2, the udev rule will then execute a python script which allows file to be copied to the USB-stick.

Problem is, I also heard that the script will be executed before the RPi mount the USB, which means the file will not be copied. Is there a solution to this? The python script was executed when i copied the file internally (on RPi itself not USB), it just doesn’t work when I tried to copy it to USB.

Below are my code:

Udev rule

JavaScript

Shell script

JavaScript

Python script

JavaScript

Advertisement

Answer

Question: … the destination is not available as the USB is yet to be mounted

Add the following to your script to verify the mount status:

JavaScript

Read this auto-mounting-usb-storage/
Maybe you can adapt to your needs.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement