Last active
August 29, 2015 14:09
-
-
Save parity3/6e0bd4979d9e808ecb86 to your computer and use it in GitHub Desktop.
stwatch (syncthing path change monitoring, cygwin inotifywait version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Windows Cygwin version. Uses inotifywait port from: https://github.com/thekid/inotify-win | |
# This version does not support a built in wait timeout for inotifywait. | |
# The solution is to use the -m switch, and keep inotifywait process spawned outputting events. | |
# For timeouts we take advantage of the -t switch for bash read. | |
# Custom values should probably not be set in here. Copy these vars to stwatch.conf to be loaded by the script. | |
APIKEY= | |
ADDRESS=127.0.0.1:8080 # default configured port for syncthing REST API | |
WATCHDIRS=( ) # These 2 arrays represent corresponding paths/folder IDs | |
WATCHREPOS=( ) | |
TIMEOUTSECONDS=20 | |
function watchdir { # 1=path, 2=folder ID | |
local EVENTS= | |
local ret= | |
local timeout_args= | |
while true ; do | |
read ${timeout_args} EVENTS ; ret=$? | |
if [ $ret = 142 ] ; then # this is what's returned on read timeout | |
wget --post-data="" --header="X-API-Key:$APIKEY" -qO- http://$ADDRESS/rest/scan?folder="$2" | |
echo "Updated repo: $2, wget returned:$?" >&2 | |
timeout_remaining= # we can wait forever until inotifywait gives us more | |
elif [ $ret = 0 ] ; then | |
timeout_args="-t ${TIMEOUTSECONDS}" | |
else | |
exit 2 # unknown response from read | |
fi | |
done < <( exec inotifywait -r -e modify,delete,create,move -m "$1" ) | |
} | |
if [ -z "$(which inotifywait)" ] | |
then | |
echo "inotifywait not found." | |
exit 1 | |
fi | |
cd "$(dirname "$0")" | |
if [ -f stwatch.conf ] | |
then | |
. stwatch.conf # allow customization of WATCHDIRS/API | |
fi | |
# spawn off watcher processes for each repo/path pair declared above | |
for i in $(seq ${#WATCHDIRS[@]}) | |
do | |
d=$(readlink -f "${WATCHDIRS[$i-1]}") | |
repo=${WATCHREPOS[$i-1]} | |
watchdir "$d" "$repo" & | |
done | |
wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to use bash read -t timeout setting, much cleaner than sleep timer signal trap method.