8

I often find myself with an incomplete or invalid emacs config, which breaks the normal behavior of kill-emacs such that it stops at an error.

In this case, I would like to forcefully kill emacs, without resorting to hunting down the PID and issuing a kill from the shell. Can I do this in pure Emacs Lisp?

8
  • I'm not sure I follow. If kill-emacs doesn't work other elisp approaches may also be fouled up, as they probably end up calling kill-emacs one way or another. What error do you get? It may be easier to fix the error.
    – Tyler
    Dec 16, 2014 at 20:37
  • I mean, you could create a function that calls kill -9 on the current Emacs pid for instance. You'd have to be very messed up to break that.
    – PythonNut
    Dec 16, 2014 at 20:38
  • 5
    You can try setting kill-emacs-hook to nil before calling kill-emacs. You can also get Emacs's pid using emacs-pid and make it commit suicide by calling call-process with "kill -9". Dec 16, 2014 at 20:43
  • 1
    if you don"t want to hunt the PID, still, there is pkill & pgrep :\
    – Nsukami _
    Dec 16, 2014 at 21:05
  • 1
    Unrelated, but you might want to avoid running multiple sessions of emacs Dec 16, 2014 at 21:11

3 Answers 3

15

You can try setting kill-emacs-hook to nil before calling kill-emacs:

(defun really-kill-emacs ()
  "Like `kill-emacs', but ignores `kill-emacs-hook'."
  (interactive)
  (let (kill-emacs-hook)
    (kill-emacs)))

Alternatively, you can get Emacs's PID using emacs-pid and make it commit suicide by calling call-process with "kill -9":

(defun brutally-kill-emacs ()
  "Use `call-process' to send ourselves a KILL signal."
  (interactive)
  (call-process "kill" nil nil nil "-9" (number-to-string (emacs-pid))))
3
  • 3
    Not a critique of the answer, just additional information: note that using signal nine (the -9 in kill -9) will orphan any processes that emacs has spawned, just as it would happen if emacs were a simple shell script.. Dec 16, 2014 at 22:29
  • Although TERM often fails to kill emacs in a broken state. So it may be the only thing.
    – PythonNut
    Dec 16, 2014 at 23:22
  • When I call this way: emacsclient -e "(really-kill-emacs)" it kills it. But when I run it again, it try to kill already killed emacs, which leads to print out log of emacsclient ending with Emacs daemon should have started, trying to connect again@Constantine
    – alper
    Jul 19, 2020 at 20:26
1

Sometimes, I end up with several unreachable emacs processes laying around. My emacsclient normally automatically starts an emacs and calls server-start--I guess there is some bug in my configuration somewhere which results in this happening more than once.

I kill the extra emacsen with killall emacs in a shell. So far, -9 hasn't been needed. (It works when there's only one emacs running, too, and I don't have to hunt down a pid!)

2
  • Sometimes, when emacs hangs completely, a -9 is needed. Of course, this means that emacs won't be able to commit suicide, but I'm adding it just in case.
    – PythonNut
    Dec 22, 2014 at 19:30
  • This was the only thing that worked for me after an ein:jupyter-notebook was preventing all other methods. I guess is was hanging somewhere, but I had already ended all Python processes and killed all buffers.
    – n1k31t4
    Nov 7, 2017 at 8:31
0

This will force kill emacs daemon from bash. If there is no emacs running and when we run emacsclient -e "(kill-emacs)", which starts emacs and kills it again. I just added simple if/else to prevent that.

  • Instead of kill-emacs you can use brutally-kill-emacs () that is defined in @Constantine's answer.
#!/bin/bash

num=$(ps aux | grep -E "[e]macs --daemon" | wc -l)
if [ $num -ge 1 ]; then
    emacsclient -e "(brutally-kill-emacs)" > /dev/null 2>&1

    # following commands guarantees 
    kill -9 $(ps aux | grep -E "emacs"      | awk '{print $2}')
    (killall emacs) > /dev/null 2>&1
    (killall emacsclient) > /dev/null 2>&1
    kill -9 $(ps aux | grep -E "[e]macs" | awk '{print $2}') > /dev/null 2>&1
else
    echo "emacs is already killed."
fi


----

If you are a m

Sometimes following zombie process remain opened and consumes all the memory so `kill -9 $(ps aux | grep -E "emacs" | awk '{print $2}')` will be your friend.


alper 4785 100.0 0.0 4461068 7860 ?? R 1:27AM 41:42.43 /usr/local/Cellar/emacs-head@26/26.3_1/Emacs.app/Contents/MacOS/Emacs -Q -l /Users/alper/.emacs.d/elpa/async-20200809.501/async.elc -batch -f async-batch-invoke

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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