2

I'm using start-process to run a process upon certain events picked up with hooks

(start-process "foo" "*Foo*" foo-command foo-args)

I would like to do 2 things with this.

  • Prevent the process from being started if it's already running
  • Print a message to *Messages* when the process is complete

How can I do this please?

  • Please do not post the same question here and to StackOverflow. Choose one. Please delete one of these posts. Thx. – Drew Jun 3 '17 at 21:40
  • 2
    Have a look at set-process-sentinel to designate a function that generates a message when finished. See also get-process and related functions: gnu.org/software/emacs/manual/html_node/elisp/… This thread has a few examples: stackoverflow.com/questions/24548536/… Google set-process-sentinel, start-process, process-filter for additional examples. – lawlist Jun 3 '17 at 21:41
  • @lawlist perhaps you could convert that to an answer, because process sentinels are the correct approach, and there isn't currently an Answer which explains that. – phils Nov 1 '17 at 9:59
1

To see if a named process is currently running, you could use the process-status function. It will return nil if the named process is not running ...

process-status is a built-in function in `C source code'.

(process-status PROCESS)

Return the status of PROCESS.
The returned value is one of the following symbols:
run  -- for a process that is running.
stop -- for a process stopped but continuable.
exit -- for a process that has exited.
signal -- for a process that has got a fatal signal.
open -- for a network stream connection that is open.
listen -- for a network stream server that is listening.
closed -- for a network stream connection that is closed.
connect -- when waiting for a non-blocking connection to complete.
failed -- when a non-blocking connection has failed.
nil -- if arg is a process name and no such process exists.
PROCESS may be a process, a buffer, the name of a process, or
nil, indicating the current buffer's process.

And as stated here, a process sentinel is a way for code to be invoked (such as displaying a message) when the process ends.

0

For your first question, you could iterate over the process-list and only run your command if the process is not found, eg.

(defun process-running-p (name)
  (cl-loop for proc in (process-list)
     if (and (string= name (process-name proc))
             (process-live-p proc))
     return t))

;; ignore if running and live
(unless (process-running-p "foo")
  ...)
  • See also the following related example that deals with similar process names due to subsequent calls before a prior process has finished; e.g., preview; preview<1>; preview<2>; etc. stackoverflow.com/a/20023781/2112489 – lawlist Jun 3 '17 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.