Skip to content

Instantly share code, notes, and snippets.

@captainsafia
Last active April 19, 2017 20:03
Show Gist options
  • Save captainsafia/7cbd4ec99ab0fe8acce7813948e1ab14 to your computer and use it in GitHub Desktop.
Save captainsafia/7cbd4ec99ab0fe8acce7813948e1ab14 to your computer and use it in GitHub Desktop.
A poor woman's filesystem watcher

To use watchman, run watchman file-to-watch sleep-time command-to-execute like so.

watchman test.txt 1 echo 'Tada!'
function watchman --description "watchman <file> <wait-time> <command>"
set initial_time (stat -f '%Z' $argv[1])
while true
set changed_time (stat -f '%Z' $argv[1])
if [ $initial_time != $changed_time ]
eval $argv[3..-1]
set initial_time $changed_time
end
sleep $argv[2]
end
end
function watchman {
initial_time=$(stat -f '%Z' $1)
while true; do
changed_time=$(stat -f '%Z' $1)
if [ $initial_time != $changed_time ]; then
eval ${@:3}
initial_time=$changed_time
fi
sleep $2
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment