pi@Charlene:~ $ sudo diff -u \ > /opt/Sam_Dev/install/device_added.sh.original \ > /bin/device_added.sh --- /opt/Sam_Dev/install/device_added.sh.original 2022-02-23 21:39:51.000000000 +0300 +++ /bin/device_added.sh 2026-09-20 22:51:00.958581707 +0300 @@ -59,7 +59,7 @@ if [ -d $target_link ] then echo "Removing USB-Drive *FOLDER* for user=$user" >>/tmp/scripts.log - rm -frd $target_link; + rmdir "$target_link"; if [ -d $target_link ] then echo "**removing FAILED $?" >>/tmp/scripts.log @@ -71,7 +71,7 @@ if [ -L $target_link ] || [ -d $target_link ] then echo "Removing USB-Drive symlink for user=$user" >>/tmp/scripts.log - rm -r $target_link; + rm -f "$target_link"; if [ -L $target_link ] || [ -d $target_link ] then echo "**removing FAILED: $?" >>/tmp/scripts.log @@ -214,11 +214,20 @@ for entry in /media/pi/* do echo "found $entry" >>/tmp/scripts.log - # attempt to umount even if it is likely to fail - umount "$entry" >>/tmp/scripts.log - echo "umount $entry: $?" >>/tmp/scripts.log - rm -r "$entry" - echo "rm $entry: $?" >>/tmp/scripts.log + + # Attempt to unmount the entry. + umount "$entry" >>/tmp/scripts.log 2>&1 + umount_return=$? + echo "umount $entry: $umount_return" >>/tmp/scripts.log + + # Never remove an entry if the unmount failed. + if [ $umount_return -eq 0 ] + then + rmdir "$entry" >>/tmp/scripts.log 2>&1 + echo "rmdir $entry: $?" >>/tmp/scripts.log + else + echo "NOT removing $entry because unmount failed" >>/tmp/scripts.log + fi done } @@ -254,4 +263,4 @@ # mount_device # curl http://127.0.0.1/_check_for_usb >> /dev/null # fi -# echo "=============" >>/tmp/scripts.log \ No newline at end of file +# echo "=============" >>/tmp/scripts.log pi@Charlene:~ $