In Leopard Apple introduced launchd which is their new system startup program. Launchd is a deamon which is a manager and scheduler. In addition to eliminating the need for crontab, which is traditionally how you would run scheduled jobs on unix systems, launchd offers you a whole slew of other events you can react to programatically. Events such as mounts, file creation and modifications. The example below demonstrates creating files in a watched folder. To do this you’ll need to create a property list file with a .plist suffix and place it in the Library/LaunchAgents/ folder for the user you can also have it work system wide for all users. Name the file com.foobar.watchmyfolder.plist re-login to your computer and you should be able to test this by placing a file in the folder $HOME/dropfolder and you should see that it in return touched a file did_it_work file in your home directory.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.foobar.watchmyfolder</string>
<key>ProgramArguments</key>
<array>
<string>touch</string>
<string>/Users/username/did_it_work</string>
</array>
<key>QueueDirectories</key>
<array>
<string>/Users/username/watchfolder</string>
</array>
<key>StartOnMount</key>
<false/>
<key>WatchPaths</key>
<array/>
</dict>
</plist>
Its kind of a bummer that the format for the .plist files is xml since other much nicer formats could have been used such as yaml or something lightweight format. But to get around that bump I installed lingon which has really open up the possibilities of launchd, it doesn’t include all the options it does include enough to get your imagination going.
More informationDuring my research on this one it seems like the inotify library is the best option which can be used directly with the inotify-tools and also through some apis (see a c example in a link below). I have scetched a bash example below which I don’t like so much since relies on a crazy while loop to do its thing which just doesn’t seem right to me … or rather I would much prefer someone hiding that mess from me. So my hope is that there is something useful out there that works with configuration files similar to the launchd on the Mac OS X.
#!/bin/sh
while inotifywait -e create ~/watchfolder
do
touch ~/watchfolder.log
`date` >> ~/watchfolder.log
done
More information
Sorry, comments are closed for this article.