Prevent script (via \immediate\write18) from running multiple times

pdftexscriptsshell-escapetex-core

How to prevent the execution of script (via \immediate\write18) multiple times when running latexmk (which calls pdftex multiple times)? The problem is that during each iteration of pdftex, the script called by \immediate\write18 is executed.

One solution, albeit very rudimentary, is to have the script create a temp file, and to stop execution if it finds it. But surely there is a more elegant way of handling this…

I'm sure it's not relevant to the question, but I'm using Linux and the script is a simple bash command.

Best Answer

One approach is to replicate what latexmk does in determining the number of pdflatex iterations required, and replicate it.

However, as commented by others, the easiest is indeed to rely of storing the status on a file and read it during execution.

Since I never clean the generated files, the approach I've followed is to check for the existence of the .fdb_latexmk (generated at the very end of running latexmk) and if it exists delete it and run the scripts. If it does not exist then do nothing.

Not the most elegant answer but does work indeed.