Monitor a Folder OSX for file changes then run a bash script [duplicate]
Possible Duplicate:
Watch Filesystem in Real Time on OS X and Ubuntu
I'm looking for a efficient method to monitor a local directory in OSX and if any files have been changed in that directory run a bash script to commit the files to github.
Any recommend tools for monitoring a directory for file changes, then triggering an action, i.e. a bash script?
Solution 1:
Using fswatch from your repository:
fswatch . 'git commit -avm "snapshot at ${date}"'
This simple example would only catch changes to files already in the repository.
Solution 2:
One option would be to just use launchd. Save a property list like this as ~/Library/LaunchAgents/com.superuser.445907.plist
, and load it with launchctl load ~/Library/LaunchAgents/com.superuser.445907.plist
or by logging out and back in.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.superuser.445907</string>
<key>Program</key>
<string>/Users/username/script</string> <!-- ~/ doesn't work -->
<key>WatchPaths</key>
<array>
<string>/Users/username/Folder/</string>
</array>
<key>ThrotteInterval</key>
<integer>0</integer> <!-- run at most every 0 seconds, by default 10 -->
</dict>
</plist>
Launchd only registers changes to files when they are saved atomically, or deleted and recreated every time they are saved. Most OS X applications perform atomic saves by default, but for example TextMate and vim don't. Changes in subfolders of watched folders aren't detected.
launchctl unload $path && launchctl load $path
applies changes to a plist.
See man launchd
and man launchd.plist
for more information.