Skip to content

Instantly share code, notes, and snippets.

@wting
Last active August 29, 2015 14:02
Show Gist options
  • Save wting/60ac617e6f20f52e273e to your computer and use it in GitHub Desktop.
Save wting/60ac617e6f20f52e273e to your computer and use it in GitHub Desktop.
Watch a file or directory for changes and run a command in response. For example: wait_do src/ ./run_tests.py
#!/usr/bin/bash
wait_do() {
if [[ `uname` == 'Darwin' ]]; then
if ! command -v fswatch &>/dev/null; then
echo "fswatch not found!"
return
fi
fswatch . ${@}
else
if ! command -v inotifywait &>/dev/null; then
echo "inotifywait not found!"
return
fi
local watch_file=${1}
shift
if [[ ! -e ${watch_file} ]]; then
echo "${watch_file} does not exist!"
return
fi
local exclude_list="(^tags$|\.git|\.pyc$|\.log$|\.yaml$|[[:digit:]])"
while inotifywait -re close_write --excludei ${exclude_list} ${watch_file} &>/dev/null; do
local start=$(\date +%s)
echo "start: $(date)"
echo "exec: ${@}"
${@}
local stop=$(\date +%s)
echo "finished: $(date) ($((${stop} - ${start})) seconds elapsed)"
done
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment