Skip to content

Instantly share code, notes, and snippets.

@theepicsnail
Created January 16, 2015 21:58
Show Gist options
  • Save theepicsnail/7931333f153c96721477 to your computer and use it in GitHub Desktop.
Save theepicsnail/7931333f153c96721477 to your computer and use it in GitHub Desktop.
Monitor files, rerun script when they are modified
#!/bin/bash
# find --
split="-1"
for (( i = 0; i < ${#@}; i++ )); do
if [ "${!i}" == "--" ]
then
split=$i
i=${#@}
fi
done
if [ "$split" == "-1" ]
then
echo "Missing --"
echo "Expected $0 files ... -- command..."
exit 1
fi
FILES=${@:1:$split-1}
shift $split
function quit () {
end
exit 0
}
trap quit SIGINT
PID=0
function end() {
if [ $PID -ne 0 ]
then
kill $PID 2>&1 >/dev/null
echo "Killed $PID"
fi
}
function begin() {
$* &
echo "Started $*"
PID=$!
}
begin $*
while true
do
inotifywait -q -e MODIFY ${FILES[@]}
end
begin $*
sleep 5
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment