1

I am writing a lisp program to be run as a batch file through emacs.

The program calls an external application which is supposed to stay running after the call to emacs finishes.

The two basic ways for emacs to call external application that I am aware of are call-process and start-process.

Since call-process creates a synchronous process, it does not meet my requirement to stay alive after the call to emacs is done, so I believe my only alternative is start-process.

However, even though start-process is supposed to create asynchronous processes, it seems like the created process is killed when emacs finishes.

Question. How to start a persistent asynchronous process trough batch in emacs, i.e, one that neither waits for, nor gets killed when emacs finishes?

In the examples below I am using, as external application, the pdf viewer Okular, but you might substitute it for any other GUI application.

;; Does not work because emacs waits for call-process to finish.
SHELL-PROMPT> emacs -Q --batch --eval '(call-process "okular" nil nil nil)'

;; Does not work because emacs apparently kills the created process upon exit
SHELL-PROMPT> emacs -Q --batch --eval '(start-process "Okular" nil "okular")'

;; Here is some more evidence that emacs kills the created process upon exit
SHELL-PROMPT> emacs -Q --batch --eval '(progn (start-process "Okular" nil "okular") (sit-for 3))'
Ruy
  • 787
  • 4
  • 11

1 Answers1

3

You can use call-process the same way as you did, but just replace the third argument by 0. If the third argument is 0, Emacs don't wait for the process and quit without killing it.

SHELL-PROMPT> emacs -Q --batch --eval '(call-process "okular" nil 0 nil)'
  • Nothing like a short and direct answer! Thanks very much!!! I guess I got confused by the description "*Call PROGRAM synchronously in separate process*" in the docstring. So in fact it is not synchronous when called with DESTINATION=0, right? – Ruy May 30 '21 at 19:38
  • Yes, I agree with you that the description can be a bit misleading. – Nicolas Mace May 31 '21 at 07:45
  • Dear Nicolas, I just realized that `start-process` ends up calling another more versatile function named `make-process`. Would you know how to solve my problem using `make-process`? That is, adding the appropriate parameters to the following so that the process does not get killed when emacs exits? `SHELL-PROMPT> emacs -Q --batch --eval '(make-process :buffer nil :name "OKULAR" :command (list "okular" "file.pdf"))'` – Ruy May 31 '21 at 14:43
  • I admit that I don't know if this is possible. – Nicolas Mace May 31 '21 at 21:02
  • Thanks anyways for considering my question. Your help was invaluable to me! – Ruy Jun 01 '21 at 01:48