I have written a bash script for the raspberry pi that copies some files to a usb device and then moves them in a new folder on the harddrive. When the usb device is plugged in, it gets mounted by a udev rule and after that the copy/move script is executed. If I wait for 5 s or so it works perfectly fine.
For a better feedback, when to unplug the device I turned an led on at the beginning and off at the end. This is not working! The led turns on, shortly after that off. The files are not copied at that moment! I have to wait another 5 s and then all is fine. Why's that? The script should run serialized or am I wrong here?
Here the script:
#!/bin/bash
# Enable LED
/home/pi/project/src/led.sh 8 1
data="/home/pi/project/data/"
olddata="/home/pi/project/data/olddata/"
backup="/media/usbstick/backup/"
dateFolder=""
# Get current time
t=$(date +"%y-%m-%d_%H-%M")
# Check backup directory
if [ ! -d "$backup" ]; then
mkdir $backup
fi
dateFolder="${backup}backup_${t}/"
# Check dataFolder directory
if [ ! -d "$dateFolder" ]; then
mkdir $dateFolder
fi
# Check olddatq directory
if [ ! -d "$olddata" ]; then
mkdir $olddata
fi
# First copy all data to usb stick
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} cp -r {} $dateFolder
# Then move the data to olddata
find "${data}" -maxdepth 1 -mindepth 1 ! -iname "olddata" \
| xargs -I {} mv {} $olddata
# Disable red LED, enable green
/home/pi/project/src/led.sh 8 0
/home/pi/project/src/led.sh 7 1
syncafter each call tofindjust in case the operation completed but fs isn't done syncing files to flash.sleep 5to your script to give it a delay also.sleepis a really bad advice and should always be avoided when possible. Always aim to get deterministic behaviour by checking states etc instead.