From cefbce656c511a3583744ed7735aa71249b73500 Mon Sep 17 00:00:00 2001 From: Robert Günzler Date: Tue, 15 Jun 2021 18:02:11 +0200 Subject: bin/latexmk: ensure there is only one instance --- bin/latexmk | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/latexmk b/bin/latexmk index 520fe20..966e058 100755 --- a/bin/latexmk +++ b/bin/latexmk @@ -7,11 +7,13 @@ ENGINE=${ENGINE:-tectonic} ENGINE_ARGS=${ENGINE_ARGS:-} VIEWER=${VIEWER:-zathura} +pidfile=/tmp/latexmk.pid +once= reexec= nocolor= while getopts 1Rnh opt; do case "$opt" in - 1) entr_args='-z' ;; + 1) once=1 ;; R) reexec=1 ;; n) nocolor=1 ;; h | ?) @@ -26,6 +28,8 @@ while getopts 1Rnh opt; do done shift $((OPTIND - 1)) +[ -n "${once}" ] && entr_args='-z' + # http://stackoverflow.com/a/18443300/441757 _realpath() { OURPWD=$PWD @@ -52,6 +56,7 @@ if [ -n "${reexec}" ]; then # compile the document log "running ${ENGINE}" + # shellcheck disable=SC2086 ${ENGINE} ${ENGINE_ARGS} "$1" if ! pgrep -fx "${VIEWER} $pdf" >/dev/null; then @@ -63,9 +68,18 @@ if [ -n "${reexec}" ]; then exit 0 fi +if [ -z "${once}" ]; then + # make sure we only run once + if [ -f "${pidfile}" ]; then + kill -TERM "$(cat "${pidfile}")" || rm "${pidfile}" + fi + printf "%d" "$$" >${pidfile} + trap '{ rm "${pidfile}"; }' EXIT +fi + if [ -z "$1" ]; then - ls *.tex + ls ./*.tex else - [ -f "$1" ] && printf "$1\n" - ls *.tex + [ -f "$1" ] && printf "%s\n" "$1" + ls ./*.tex fi | entr ${entr_args} -ns 'latexmk -R $0' -- cgit 1.4.1