diff --git a/monitor.sh b/monitor.sh new file mode 100644 index 0000000..6c356c6 --- /dev/null +++ b/monitor.sh @@ -0,0 +1,48 @@ +#!/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}