Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created May 13, 2011 16:59
Show Gist options
  • Save djspiewak/970887 to your computer and use it in GitHub Desktop.
Save djspiewak/970887 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
if ARGV.size < 2
$stderr.puts 'usage: monitor <path> <command> [arg1 arg2 ...]'
exit 100
end
PATH = ARGV[0]
COMMAND = ARGV[1]
ARGS = ARGV[2..-1]
INDEX = {}
def scan(path)
if File.exists? path
if File.directory? path
children = Dir.entries path
children.delete '.'
children.delete '..'
results = children.map { |f| scan "#{path}/#{f}" }
results.any? { |r| r }
else
if INDEX[path] != (File.mtime path)
INDEX[path] = File.mtime path
true
else
false
end
end
else
false
end
end
puts "Monitoring path '#{PATH}' for changes..."
while true
if scan PATH
system COMMAND, *ARGS
puts
puts "Monitoring path '#{PATH}' for changes..."
end
sleep 3
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment