Skip to content

Instantly share code, notes, and snippets.

@zph
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save zph/18532e0797ee7608ee06 to your computer and use it in GitHub Desktop.

Select an option

Save zph/18532e0797ee7608ee06 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# AUTHOR: Zander : zander@xargs.io : @_ZPH
# LICENSE: MIT
# Credit for initial script: http://stackoverflow.com/a/18341108/1930671
set -o nounset
set -o pipefail
set -o errexit
# set -o xtrace
set -x
readonly PROGNAME=$(basename $0)
readonly ARGS="$@"
readonly TO_MONITOR="$1";shift
readonly CMD_TO_RUN="$*"
readonly PID="$$"
PRIOR_ATIME="0"
set +x
run_loop(){
while true
do
ATIME=`stat -f %Z ${TO_MONITOR}`
if ! [[ $ATIME == $PRIOR_ATIME ]];then
echo "CMD: ${CMD_TO_RUN}"
$(${CMD_TO_RUN})
PRIOR_ATIME=$ATIME
fi
sleep 1
done
}
usage(){
echo "$PROGNAME [file-to-watch] [*cmd-to-run]"
exit 1
}
main(){
if [[ $# -lt 2 ]];then
usage
fi
if [[ $CMD_TO_RUN == "" ]];then
echo "No cmd to execute"
exit 1
fi
run_loop
}
main $ARGS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment