#!/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 | |
) |
This comment has been minimized.
This comment has been minimized.
15 minutes !/bin/bashif [ "$1"x = ""x ]; then Usage: DetectChanges "[ directory to watch ]" "[ command/script to execute ]" "[optional: time in second between executes]" |
This comment has been minimized.
This comment has been minimized.
Unfortunately |
This comment has been minimized.
This comment has been minimized.
@ArminVieweg Did you eventually find a solution for that? It's precisely what I was looking to do. |
This comment has been minimized.
This comment has been minimized.
The version with |
This comment has been minimized.
This comment has been minimized.
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? |
This comment has been minimized.
This comment has been minimized.
why not use watch in homebrew?
source code here:https://gitlab.com/procps-ng/procps |
This comment has been minimized.
This comment has been minimized.
Because not everyone runs Mac? |
This comment has been minimized.
This comment has been minimized.
Why can't I kill it with ctrl + c? |
This comment has been minimized.
This comment has been minimized.
Nice script! Thanks |
This comment has been minimized.
This comment has been minimized.
Cool idea, but how would you pass extra arguments with quotes? say i want |
This comment has been minimized.
This comment has been minimized.
Thanks for the script! I'm having trouble figuring out where the 1 second is defined, as I'd like to extend it. |
This comment has been minimized.
This comment has been minimized.
@tdmalone, |
This comment has been minimized.
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"