| #!/bin/bash | |
| # | |
| # Watch current directory (recursively) for file changes, and execute | |
| # a command when a file or directory is created, modified or deleted. | |
| # | |
| # Written by: Senko Rasic <senko.rasic@dobarkod.hr> | |
| # | |
| # Requires Linux, bash and inotifywait (from inotify-tools package). | |
| # | |
| # To avoid executing the command multiple times when a sequence of | |
| # events happen, the script waits one second after the change - if | |
| # more changes happen, the timeout is extended by a second again. | |
| # | |
| # Installation: | |
| # chmod a+rx onchange.sh | |
| # sudo cp onchange.sh /usr/local/bin | |
| # | |
| # Example use - rsync local changes to the remote server: | |
| # | |
| # onchange.sh rsync -avt . host:/remote/dir | |
| # | |
| # Released to Public Domain. Use it as you like. | |
| # | |
| EVENTS="CREATE,CLOSE_WRITE,DELETE,MODIFY,MOVED_FROM,MOVED_TO" | |
| if [ -z "$1" ]; then | |
| echo "Usage: $0 cmd ..." | |
| exit -1; | |
| fi | |
| inotifywait -e "$EVENTS" -m -r --format '%:e %f' . | ( | |
| WAITING=""; | |
| while true; do | |
| LINE=""; | |
| read -t 1 LINE; | |
| if test -z "$LINE"; then | |
| if test ! -z "$WAITING"; then | |
| echo "CHANGE"; | |
| WAITING=""; | |
| fi; | |
| else | |
| WAITING=1; | |
| fi; | |
| done) | ( | |
| while true; do | |
| read TMP; | |
| echo $@ | |
| $@ | |
| done | |
| ) |
hydranix
commented
Nov 26, 2013
|
15 minutes !/bin/bashif [ "$1"x = ""x ]; then Usage: DetectChanges "[ directory to watch ]" "[ command/script to execute ]" "[optional: time in second between executes]" |
ArminVieweg
commented
Feb 19, 2015
|
Unfortunately |
phazei
commented
Apr 7, 2015
|
@ArminVieweg Did you eventually find a solution for that? It's precisely what I was looking to do. |
vn971
commented
Apr 20, 2015
|
The version with |
shotputty
commented
Sep 1, 2015
|
What will be the benefit of running onchange.sh every x minutes (to trigger rsync) versus running rsync every x minutes? Is it just less demanding on the server? |
noscripter
commented
Dec 9, 2015
|
why not use watch in homebrew?
source code here:https://gitlab.com/procps-ng/procps |
joshuataylor
commented
Jan 5, 2016
|
Because not everyone runs Mac? |
hkirsman
commented
Feb 21, 2016
|
Why can't I kill it with ctrl + c? |
Pitt-Pauly
commented
Jul 29, 2017
|
Nice script! Thanks |
mr-moon
commented
Nov 4, 2017
|
Cool idea, but how would you pass extra arguments with quotes? say i want |
evgenius commentedJul 17, 2013
If you change "$@" to "eval $@" you will be able to run several commands.
Example:
onchange.sh "rsync dest orig && notify-send Sucess || notify-send Failure"