Search code examples
macoslaunchd

How can I pass the name of the file that was changed in launchd?


I am trying to watch a directory for changes via launchd. My plist file looks like this:

<key>ProgramArguments</key>
<array>
  <string>/Users/myname/bin/boink</string>
  <string>path modified</string>
</array>

All this works OK, but I would like to pass the name of the file that was changed as an argument to the script /Users/myname/bin/boink

Is that possible? the man page isn't very helpful, nor did googling help a lot.

Thanks.


Solution

  • The short answer is: no. launchd(8) uses Kqueue (http://en.wikipedia.org/wiki/Kqueue) to receive this kind of notification. Unfortunately kqueue(2) does not return which item has triggered the event.

    You may want to use the launchd(8) key QueueDirectories instead. It works essentially the same way WatchPaths works, but it assumes that the processing agent/daemon is moving the processed items from the directory being monitored to another one. So whenever an event is triggered your job can process every file in the monitored directory. Just make sure you move them after processing.