Last active
August 29, 2015 14:02
-
-
Save zph/18532e0797ee7608ee06 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/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