bash-filesystem-monitoring/monitor.sh
2025-04-02 13:37:23 -06:00

49 lines
1.0 KiB
Bash

#!/bin/bash
# https://stackoverflow.com/a/1482133
SCRIPT_DIR="$(dirname -- "$(readlink -f -- "$0";)";)"
cd "${SCRIPT_DIR}"
# Needed for detecting a change in a directory:
apt install -y inotify-tools
# Needed for making the JSON safe:
apt install -y jq
# https://stackoverflow.com/a/2173421
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
CHILD_PIDS=""
{
while true
do
if [[ -f to_discord ]]
then
FILE_DATA="\`\`\`\n$(cat to_discord)\n\`\`\`"
printf "$FILE_DATA" > to_discord
CONTENT="$(cat to_discord | jq -Rsa .)"
curl \
-i \
-H "Content-Type: application/json" \
-X POST \
--data "{\"content\": $CONTENT}" \
"PUT WEBHOOK URL HERE"
rm to_discord
fi
sleep 10
done
} &
CHILD_PIDS="${CHILD_PIDS} $!"
while true
do
# --format "%w%f"
LINE="$(inotifywait -r -q -e modify -e create -e delete /storage)"
echo "$LINE" >> to_discord
done
wait ${CHILD_PIDS}