Skip to content

Instantly share code, notes, and snippets.

@parity3
Last active August 29, 2015 14:09
Show Gist options
  • Save parity3/6e0bd4979d9e808ecb86 to your computer and use it in GitHub Desktop.
Save parity3/6e0bd4979d9e808ecb86 to your computer and use it in GitHub Desktop.
stwatch (syncthing path change monitoring, cygwin inotifywait version)
#!/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
@parity3
Copy link
Author

parity3 commented Nov 14, 2014

Updated to use bash read -t timeout setting, much cleaner than sleep timer signal trap method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment