-1

After much trouble I got the UDEV rule to run after inserting an USB. It runs a program to convert the names of pictures and movies. I use {} & to run the program in the background: The only thing is that by unplugging the usb it is easy corrupted. So I would like to also run fsck. Does anybody has an idea?

Here is the UDEV rule:

CTION=="add", SUBSYSTEM=="block", ATTRS{idVendor}=="14cd", ATTRS{idProduct}=="121f", RUN+="/home/pi/bashtest.sh"

Here is the program:

#!/bin/bash
sudo umount /dev/sda1
sudo fsck -y /dev/sda1
{
dd=1234567890aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
sleep 5
sudo mount -t vfat /dev/sda1 /media/usb1
cd /media/usb1/DCIM/Camera
sudo find /media/usb1/DCIM/Camera -regextype posix-egrep -regex ".*[^/]{13}.JPG"|
for i in *.JPG
do
ddate=$(exiv2 "${i}"|grep timestamp)
SPEC=$ddate
read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.JPG"
find . -name '*.JPG' -size -1 -delete
done
for i in *.MP4
do
#exiftool -createdate -S -s 20140308_133017.MP4
dddate=$(exiftool "${i}" |grep "Media Create Date" | awk -F':' '{print $2, $3, $4, $5, $6, $7}')
SPEC=$dddate
read YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
d1=${YEAR:2}
d2=${dd:(10#$MONTH-1):1}
d3=${dd:(10#$DAY-1):1}
d4=${dd:(10#$HOUR-1):1}
d5=${dd:(10#$MINUTE-1):1}
d6=${dd:(10#$SECOND-1):1}
d7=0
sudo cp -nrv --preserve=all "$i" /media/usb1/DCIM/"${d1}${d2}${d3}${d4}${d5}${d6}${d7}.MP4"
done
sudo umount -l /media/usb1
sleep 5
sudo shutdown -h now
} &

Probably the code can be written better, but it works for me.

Helfenstein
  • 315
  • 1
  • 4
  • 13
  • 1
    You want to run `fsck` everytime you plug in a USB mass storage device as a "fix" to corruption that occurs when you unplug them while they are still being written to? Oh my, don't fix the symptom, fix the cause otherwise you **will** lose data sooner or later. You might also want to google *XY problem*. – Adrian Frühwirth Mar 30 '14 at 16:13
  • Let's state it this way, my parents use it in Windows and probably don't always plug it out properly and I noticed it creates sometimes this fault. So I would like to check it always to be sure. Secondly of course I unmount it properly (as far as I know) as the code shows. So, no data is still being written to the SD when it is unplugged. – Helfenstein Mar 30 '14 at 19:33
  • That's all fine and good, but running `fsck -y` can easily cause data loss, especially if there is already corruption on it. Periodically backing up and only then running `fsck` would be a better bet. Also, I think this question is technically better suited for [SuperUser](http://superuser.com) Also, I'm not seeing what doing `{..}&` would actually benefit since you're running it as the last command in a script. And including a `shutdown` call seems very strange. – Reinstate Monica Please Mar 30 '14 at 19:44
  • Thank you for your reply. I am running the program from an UDEV rule. {} & helps to run the program at the background, otherwise the startup (of the Raspberry) will give errors to not being able to finish it totally. The shutdown is for the user to see that the program is finished. Backing it up is not an option, because of the space (many pictures and movies) on the SD cards will not be enough. – Helfenstein Mar 31 '14 at 06:04
  • Last time I checked Windows automatically disables write caching on USB devices so it should not be a problem to just remove them unless you do so while they are still being written to. You can check this in *device manager -> disk drives -> properties (on of the device in question) -> policies (quick removal vs. better performance)*. Not sure if this is true for hard disks (probably not) but definitely for flash sticks and memory cards. – Adrian Frühwirth Mar 31 '14 at 06:06
  • That is a good idea, but I also still would like to have this check of fsck in it or something alike to be sure there are no errors created. – Helfenstein Mar 31 '14 at 06:09
  • What I am saying is that errors should not occur in the first place but since it does what you are telling us cannot be the whole story. Either write caching is turned on for the device in question (which it shouldn't be per default; check if somebody turned it on!) or your parents do unplug the device while it's still being written to. – Adrian Frühwirth Mar 31 '14 at 08:32
  • Tonight I can put the whole code and UDEV rule. The whole story is that my parents make pictures and videos on their Samsung. This is in the date + time format instead of the 8 character format. This SD card I put in a USB stick. This USB stick goes into a Raspberry. I created an UDEV-rule which starts this Bash program. The Bash program convert the filename in a 8 character title. My parents put the SD-card in an adapter and in an IPAD, but also looks the files on Windows on their laptop. I noticed with my Windows that the card sometimes gives an error message (card corrupted)... – Helfenstein Mar 31 '14 at 09:42
  • after converting the files with the Raspberry, while I think I properly mount and unmount the SD card with the program. But I will look tonight into your advice about the Windows setting. – Helfenstein Mar 31 '14 at 09:48
  • I have added the whole part of the code. It runs good without the fsck part. – Helfenstein Mar 31 '14 at 19:14

1 Answers1

0

Using {} && for fsck and the rest {} & let it all work on the background and helped me to get it to work!

Helfenstein
  • 315
  • 1
  • 4
  • 13