1

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
4
  • 1
    Try adding a sync after each call to find just in case the operation completed but fs isn't done syncing files to flash. Commented Jun 26, 2014 at 9:59
  • You can add a sleep 5 to your script to give it a delay also. Commented Jun 26, 2014 at 9:59
  • @JohnC Adding arbitrary sleep is a really bad advice and should always be avoided when possible. Always aim to get deterministic behaviour by checking states etc instead. Commented Jun 26, 2014 at 10:00
  • The sync option was the solution. Even the folders I intented to create with mkdir wasn't there after the script finished. One "sync" before disabling the led solved it! Thanks! Commented Jun 26, 2014 at 10:18

1 Answer 1

1

Adding a sync at the end solved it. Thanks to Jite

# 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

sync

# Disable red LED, enable green
/home/pi/project/src/led.sh 8 0
/home/pi/project/src/led.sh 7 1
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.