Sometimes I want to edit some file which is then transformed to some kind of “target” format, suitable for watching. It might be a LaTeX file, a Markdown file converted to HTML, or a HTML file converted to PDF (as it was the case for me some time ago). In the old days, I used to write something like
while [ true ]; do pdflatex file.tex; sleep 8; done
but this is of course terribly inefficient – it just runs LaTeX in fixed intervals instead of when the file is saved, which means that usually the LaTeX run will be unnecessary, and when I save the file, I might wait as much as about 8 seconds for the pdf file to be generated. (It has an additional problem of breaking in the case of an error in my source file – that is, however, easy to remedy with -interaction=nonstopmode
.)
Nowadays, knowing just a little bit more about the tools my OS gives me, I use the inotify system. To make use of that, install inotifytools
and write something like this:
while inotifywait -e modify file.tex; do pdflatex -interaction=nonstopmode file.tex; done
Here is what happens: inotifywait
, well, waits until for some kind of event (in this case, modification of file.tex
(this part can be omitted and then it waits for any event regarding this file, which would also work, but it might recompile too often, e.g., on an access
event). Then, it exits with the exit status of 0
(meaning “true” in shell scripts), so pdflatex
runs and then the loop starts over again, waiting for the next modify
event. (You can break out of this loop by either deleting file.tex
or plain old C-c
(that is, Ctrl-C
) in the terminal where the loop runs.