Here's a somewhat silly implementation of a function that does a call-process
and returns its pid (sort-of):
(defun call-process-pid ()
(let (l1 l2)
(setq l1 (list-system-processes))
(call-process "sleep" nil 0 nil "60")
(setq l2 (list-system-processes))
(cl-set-difference l2 l1)))
It's a somewhat cleaner way of doing a ps
before and after to find out what process(es) got added.
The limitation is that between the first and second calls to list-system-processes
, a bunch of processes may have been created, so the set difference will have more than one entry: AFAIK, there is no way to know which one is "your" process without looking at each one (e.g. with process-attributes
to find out the command that was run). It may also happen that your command runs to completion before the second list-system-processes
, in which case it will not appear at all - but that's probably OK for your intended use.
There are ways to daemonize a subprocess: that can be easily done from a C program (Stevens' "Advanced Programming in the Unix environment" has examples, but googling "daemonize Unix" should provide plenty). I'm not sure whether you can do the equivalent from within Emacs, but you don't have to: you can use nohup
to prevent the program from responding to SIGHUP
, so instead of (call-process "command" nil 0 nil args...)
, you can do (call-process "nohup" nil 0 nil "command" args...)
. I have not tested that it actually works, but I can't see any reason why it shouldn't. Assuming that it works, adapting this idea to your previous question should be straightforward.
call-process
is used when you want to run something and either wait for it to finish (in which case you can check exit status) or let it go async and forget about it. It does not give you any handles to manipulate the process. You'll have to use some of the higher level process primitives for that. But it is only an impression, so maybe I'm missing something.call-process
(especially with the parameter DESTINATION set to 0). The trouble I'm having is that the alternative, namelystart-process
, shuts down when emacs finishes as discussed in my previous question emacs.stackexchange.com/questions/65090/… Should you be able to figure out a way to keep a process started bystart-process
running after emacs finishes (i.e. to answer my other question), it would fully solve my problem!