找到你要的答案

Q:Starting and closing applications in Makefile

Q:启动和关闭应用程序在makefile

I use a Makefile to compile latex. In the end of the compile process I have the following rule

$(PDFREADER) $(SOURCE).pdf &

where it will open in evince the newly created pdf.

What I want is that next time I compile the pdf, the Makefile closes the current evince process, opened in during previous the compilation.

I read this and this posts, but I did not understand how to do it.

Any help?

Thanks

EDIT this is the makefile I use

SOURCE=PEI
PDFREADER=evince
EXTENSIONS=ps dvi log out toc aux bbl blg idx glg glo ist lof acn ilg ind ist lot xdy synctex.gz glsdefs

$(SOURCE).pdf:  $(SOURCE).tex  bibliografia.bib
    if [ -a evince.PID ]; then \
        kill -TERM $$(cat evince.PID) || true; \
    fi;
    pdflatex -shell-escape $(SOURCE).tex
    bibtex $(SOURCE)
    pdflatex -shell-escape $(SOURCE).tex
    pdflatex -shell-escape $(SOURCE).tex

vis: $(SOURCE).pdf
    $(PDFREADER) $(SOURCE).pdf & echo $$! > evince.PID

clean: organized
    @rm -f $(SOURCE).pdf

organized:
    @rm -f $(foreach postfix, $(EXTENSIONS), $(SOURCE).$(postfix))
    @rm -f ./*~
    @cd ./tex && rm -rf $(foreach postfix, $(EXTENSIONS), *.$(postfix)) && rm -f ./*~

我用一个makefile编译乳胶。在编译过程结束时,我有以下规则

$(PDFREADER) $(SOURCE).pdf &

它将开启表示新创建的PDF。

我希望下次我编译的PDF文件,makefile文件关闭当前显示的过程中,打开了以前在编译。

我读过这个帖子,但是我不明白怎么做。

任何帮助吗?

谢谢

这是我使用Makefile编辑

SOURCE=PEI
PDFREADER=evince
EXTENSIONS=ps dvi log out toc aux bbl blg idx glg glo ist lof acn ilg ind ist lot xdy synctex.gz glsdefs

$(SOURCE).pdf:  $(SOURCE).tex  bibliografia.bib
    if [ -a evince.PID ]; then \
        kill -TERM $$(cat evince.PID) || true; \
    fi;
    pdflatex -shell-escape $(SOURCE).tex
    bibtex $(SOURCE)
    pdflatex -shell-escape $(SOURCE).tex
    pdflatex -shell-escape $(SOURCE).tex

vis: $(SOURCE).pdf
    $(PDFREADER) $(SOURCE).pdf & echo $$! > evince.PID

clean: organized
    @rm -f $(SOURCE).pdf

organized:
    @rm -f $(foreach postfix, $(EXTENSIONS), $(SOURCE).$(postfix))
    @rm -f ./*~
    @cd ./tex && rm -rf $(foreach postfix, $(EXTENSIONS), *.$(postfix)) && rm -f ./*~
answer1: 回答1:

Once you start your process you have to store the process id (pid) to a file, let me call it evince.PID. So change your line in:

$(PDFREADER) $(SOURCE).pdf & echo $$! > evince.PID

This saves the pid in the evince.PID file. To stop the previous process just read the pid from that file (if exists) and kill the corresponding process. To do this add to you makefile:

if [ -a evince.PID ]; then \
    kill -TERM $$(cat evince.PID) || true; \
fi;

For future makefile/bash writing notice that:

  • $$! corresponds to the bash variable $!, as the makefile syntax also indicates variables with $ to disambiguate a bash variable we need a double $$
  • $$(cat evince.PID) reads the file into a bash variable, again marked with $$
  • kill -TERM returns a non 0 exit code if the process does not exist (e.g. you already killed it by hand). With || true a 0 exit code is returned even if kill failed, so a failed kill does not cause the makefile to fail

Edit:
My bad, I figured out that -a to check if a file exists is now deprecated and should be avoided in favor of -e. Debian Almquist shell (dash) seems to be a bit picky about this. To kill the process then use:

if [ -e evince.PID ]; then \
    kill -TERM $$(cat evince.PID) || true; \
fi;

一旦你开始你的过程你存储进程ID(PID)到一个文件,让我把它evince.pid。所以改变你的路线:

$(PDFREADER) $(SOURCE).pdf & echo $$! > evince.PID

这节省了在evince.pid文件PID。要停止上一个进程,只需从该文件中读取PID(如果存在),并杀死相应的进程。这样做增加你的makefile:

if [ -a evince.PID ]; then \
    kill -TERM $$(cat evince.PID) || true; \
fi;

未来的makefile / bash写通知:

  • $$! corresponds to the bash variable $!, as the makefile syntax also indicates variables with $ to disambiguate a bash variable we need a double $$
  • $$(cat evince.PID) reads the file into a bash variable, again marked with $$
  • kill -TERM returns a non 0 exit code if the process does not exist (e.g. you already killed it by hand). With || true a 0 exit code is returned even if kill failed, so a failed kill does not cause the makefile to fail

Edit:
My bad, I figured out that -a to check if a file exists is now deprecated and should be avoided in favor of -e. Debian Almquist shell (dash) seems to be a bit picky about this. To kill the process then use:

if [ -e evince.PID ]; then \
    kill -TERM $$(cat evince.PID) || true; \
fi;
makefile