37

How to kill an internal process in Emacs? For example I run M-x shell.

I can check running processes with M-x list-processes but how can I kill a process from this list?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Linkas
  • 906
  • 2
  • 10
  • 26

5 Answers5

30

There is no default key binding for this; however see pjammer's answer -- list-processes+ includes (among other things) a kill binding on C-k -- and also Joao Tavora's answer -- which provides just a kill binding (for the same key).

event_jr points out in the comments that you can use M-: (kill-process) RET to kill the current buffer's process.

More generally: You can use M-: (kill-process PROCESS) RET, where PROCESS "may be a process, a buffer, or the name of a process or buffer", with those names being as they appear in the output of list-processes. Process names take precedence over buffer names, should you happen to have a conflict; so it's probably best to be in the habit of supplying the process name.

Alternatively, Emacs 23+ has a general system process manager (M-x proced) which is more akin to running top, and which does have a default binding for sending (arbitrary) signals (k). Of course it may be far less obvious in that listing which process you're interested in.


Edit: Better late than never :) The following enables M-x kill-process RET to be used (tested in Emacs 26.1):

;; Enable M-x kill-process (to kill the current buffer's process).
(put 'kill-process 'interactive-form
     '(interactive
       (let ((proc (get-buffer-process (current-buffer))))
         (if (process-live-p proc)
             (unless (yes-or-no-p (format "Kill %S? " proc))
               (error "Process not killed"))
           (error (format "Buffer %s has no process" (buffer-name))))
         nil)))
phils
  • 71,335
  • 11
  • 153
  • 198
  • 6
    if you're in the buffer that has the process associated, then `(kill-process)` will work without a buffer specification. – event_jr May 17 '12 at 06:05
  • this doesn't seem to apply if the process doesn't have an associated buffer (anymore), like when you killed the buffer but that didn't trigger the killing of the actual process, which can be the case sometimes. – Erik Kaplun Oct 03 '15 at 08:59
  • `(kill-process ...)` didn't work for me on a result from `(make-network-process ...)`. [Paulo Tomé's answer](http://stackoverflow.com/a/16524732/2392683) to use `(delete-process ...)` was the trick. Looks like list-processes+ also uses `(delete-process ...)`. – chwarr Feb 17 '16 at 04:55
  • What does the setting the property `interactive-form` do? This looks like `advice` type of functionality but can you please explain how it works? It seems it would change the definition as if `interactive (...)` was specified for the function? So does this basically make `kill-process` interactive and prompt user before killing? Is the `nil` at the end required? – Miserable Variable Sep 08 '20 at 16:53
  • 1
    @MiserableVariable Yes, this is assigning an `(interactive...)` form to the function, to make it a command. Everything preceding the `nil` in my code is just choosing whether to signal an error before the rest of the function gets a chance to run. If it gets past that, the `nil` is the actual argument list I'm returning for an interactive call (I.e. an empty list, as the function's arguments are all optional, and I'm not populating any of them). If I wasn't explicit about that, then I'd be at the mercy of whatever value the preceding form had returned. – phils Sep 08 '20 at 23:10
  • You can also return `(list proc)` instead of `nil` in order to use the process object shown in the interactive prompt explicitly rather than implicitly. The result is the same, but the explicit version makes things more obvious. – phils Sep 09 '20 at 04:56
  • `M-x kill-process` is a command by default in Emacs 29+. – phils Jan 24 '22 at 01:15
20

This thread is ancient but here's a very quick hack that works perfectly for me

(define-key process-menu-mode-map (kbd "C-k") 'joaot/delete-process-at-point)

(defun joaot/delete-process-at-point ()
  (interactive)
  (let ((process (get-text-property (point) 'tabulated-list-id)))
    (cond ((and process
                (processp process))
           (delete-process process)
           (revert-buffer))
          (t
           (error "no process at point!")))))
joao
  • 3,517
  • 1
  • 31
  • 43
11

An alternative way:

You can use M-x eval-expression RET

Then type: (delete-process "<name-of-the-process>") RET

(where "name-of-the-process" was previously obtained from M-x list-processes RET).

Confirm that the process was killed by repeating M-x list-processes RET).

And that's it.

Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
3

it looks like there is a new mode or add on you can use instead called list process +

pjammer
  • 9,489
  • 5
  • 46
  • 56
0

If you use counsel you can run M-x counsel-list-processes. You can then type M-o to bring up actions, one of which is kill.

guibor
  • 550
  • 5
  • 6