0

I'm compiling a latex file in a script and then I'm trying to either open a new pdf reader window or reload the current one (llpp being my reader), so after compiling my script does

pdflatex $FILE_NAME
LLPP_ID=$(pgrep llpp)
if [ $LLPP_ID ]
then
kill -HUP $LLPP_ID
else
llpp $FILE_PATH"/"$FILE_NAME".pdf" &
fi

which works as intended as long as there is only one llpp window open. I would like to fix this to work for any number of llpp windows open.

Ideally I would fix the PID of the process from the bash script, but I understand this is not possible as PID are assigned by the kernel.

I thought I could use environment variable, but than they would remain available for subshells, but then I could run in a situation where I compile the file, store the PID into a env variable, close the terminal and lose the env variable while the LLPP windows remains open but I don't know where to look for it's PID anymore.

How could I do this?

1
  • 1
    Instead of trying to find the right llpp to kill -HUP, you could use this wrapper which reloads PDFs in llpp directly. That only handles part of the problem but it might be enough... Nov 7, 2018 at 18:19

2 Answers 2

2

Simplistically, you could adjust the logic to say:

  • Is there an llpp process that's opened with this filename?
  • If so, HUP it.
  • If not, create one.

New code:

pdflatex "$FILE_NAME"
if ! pkill -HUP -f "llpp ${FILE_PATH}/${FILE_NAME}.pdf"; then
  llpp "${FILE_PATH}/${FILE_NAME}.pdf" &
fi
0
2

The issue is with the test on the unquoted $LLPP_ID variable. When there are two PIDs, the test would expand to a syntax error (something like [ 12334 24433 ]).

Instead, use the -n test to test for a non-empty string (and always quote any variable expansion):

if [ -n "$LLPP_ID" ]; then

But it would be easier with pkill:

#!/bin/sh

pdflatex "$filename"
if ! pkill -HUP llpp; then
    llpp "$filepath/$filename.pdf" &
fi

This would typeset your document, send HUP to any running llpp instances no matter if there are one or many, and if there aren't any, start a new one.

Note that I know nothing about what the llpp PDF reader is or does.

1
  • This still has the disadvantage of not knowing whether the document is open anywhere; if an llpp instance is already running with another document open, the newly built document won’t be opened at all. Nov 7, 2018 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .