#!/bin/bash
while true; do
# Detect the mount point of the external HDD
MOUNT_POINT=$(lsblk -o MOUNTPOINT | grep -E '/media/rosika/backups')
#Why use awk just to filter out UUID, when it's easier to omit from lsblk?
if [ ! -z "$MOUNT_POINT" ]; then
# Define the file path on the external HDD
FILE_PATH="$MOUNT_POINT/keep_alive.txt"
# Write the word "Hello" to the file
echo "Hello" >> "$FILE_PATH"
# Flush the file system buffers
sync
# Truncate the file if it gets too large
if [[ $(stat -c%s "$FILE_PATH") -gt 1024 ]]; then
> "$FILE_PATH"
fi
else
echo "HDD not mounted."
fi
# Sleep for 50 seconds
sleep 50
done
ChatGPT can write buggy script too
I’d do it differently:
That checks the mount point in the loop, and whenever you unmount the drive it won’t be defunct…
But I can write buggy script too, so double check whether my modifications are appropriate!