Fork me on GitHub

Caffeinated Bitstream

Bits, bytes, and words.

File event notifications in Mac OS April 7, 2011

While using Mac OS, I've been missing the handy Linux inotifywait utility—it's a simple program to use the Linux inotify facility to wait on certain file events. I sometimes write scripts that use inotifywait to automatically launch programs when files are changed. For instance, I can have a script automatically compile a program whenever I save the source file in the editor.

It turns out that Mac OS and other recent BSD operating systems have a similar kernel facility called kqueue, and it was really easy to whip up a small program to block until an event occurs on a file. My filewait program is linked below, and can be used in scripts such as this one:

#!/bin/sh
# wait for the file to change...
while filewait magnumopus.tex; do
    # compile the file
    sleep 0.2
    pdflatex magnumopus.tex
done

Downloads

  • filewait.c - My simple program to pause until a file event occurs.
Comments:

Thanks for the utility! This was exactly what I was looking for in searching for a quick mac file watching tool.

Posted by Mike H on September 15, 2012 at 07:00 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed