Skip to main content

Using inotify from the command line

As I really like the simplicity and quality of ReStructeredText, I do more of my notes intended also for other people in it. For small texts, rst2pdf does a good job and is so simple that it can easily be called from the command line without further ado. But still, it is a traditional "edit-compile-debug" cycle that surely can be automated.

Of course under Linux there is the powerful inotify mechanism, but until now I did not use that directly from the command line. Some apt-cache browsing later I installed the "inotify-tools" package containing the small inotifywait command line utility.

With that in place, one can do a simple two-liner like this:

( while : ; do rst2pdf doc.rst ; inotifywait doc.rst ; done ) & \
  sleep 1 ; evince doc.pdf

This will process the file and infinitely rerun rst2pdf again when the source file has changed. The sleep 1 is intended to wait for the first rendering of the file so that evince can start successfully. Of course the one second constant is ugly and surely one can get rid of it, but that requires significantly more characters and thus blows the scope of this short entry.

It remains to be said that evince is clever enough to monitor the PDF file by itself, so it automatically updates the display when a new rendering is done. Thus with this simple while loop we have created a "fully event driven" rendering pipeline without any polling and saving the file in the editor automatically updates the PDF window.

Comments

Comments powered by Disqus