Skip to content

Instantly share code, notes, and snippets.

@mxcl
Created July 18, 2009 09:32
Show Gist options
  • Save mxcl/149491 to your computer and use it in GitHub Desktop.
Save mxcl/149491 to your computer and use it in GitHub Desktop.
rsync whenever the directory the script is in changes using the OS X FSEvents API
#!/usr/bin/ruby
require 'osx/foundation'
OSX.require_framework '/System/Library/Frameworks/CoreServices.framework/Frameworks/CarbonCore.framework'
include OSX
$d=File.basename Dir.pwd
Dir.chdir '..'
fsevents_cb = proc do |stream, ctx, numEvents, paths, marks, eventIDs|
system "rsync --delete -rt --rsh=ssh '#{$d}' 'methylblue.com:public_html/'"
end
stream = FSEventStreamCreate(
KCFAllocatorDefault,
fsevents_cb,
nil,
[$d],
KFSEventStreamEventIdSinceNow,
1.0,
0)
die "Failed to create the FSEventStream" unless stream
FSEventStreamScheduleWithRunLoop(
stream,
CFRunLoopGetCurrent(),
KCFRunLoopDefaultMode)
die "Failed to start the FSEventStream" unless FSEventStreamStart(stream)
CFRunLoopRun()
@sanjayayogi
Copy link

As a linux/unix person and now using Mac OS X Snow Leopard, I have been looking for a Mac OS X equivalent to inotify. I typically would use inotify to monitor changes in a folder and then use that to trigger rsync.

This looks like the same type of idea. I am a ruby nube. Can you give me an example of how you are using this file, are you using it as a process that syncs two directories ie sync the local directory to the remote?

Question:
From the ruby code - how are you setting the base directory (local) - Do you cd into the folder you want to watch and run the script from there?

Question:
Does this script watch for changes in sub-directories as well?

Question:
How do you run the code? IS THIS CORRECT?
cd ./folder2watch
ruby rsync+watcher.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment