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.