How to gracefully shutdown emacs daemon? [closed]

Solution 1:

ShreevatsaR is right, the answer is kill-emacs or save-buffers-kill-emacs, both of which are interactive, and so can be run from within Emacs with M-x save-buffers-kill-emacs. This is probably the best way to do it, since you will get to save modified files.

Another alternative is to make a shell file like this:

#!/bin/bash
emacsclient -e "(kill-emacs)"

Which you can run from wherever you like (menu icon, panel, etc).

Solution 2:

This linuxquestions.org page has a Python script that can be run during login that listens for the 'save yourself' event that Gnome emits during shutdown. You could modify that to do the:

emacsclient -e '(save-buffers-kill-emacs)'

Official docs: https://www.emacswiki.org/emacs/EmacsAsDaemon#toc8

Solution 3:

Another addendum to ShreevatsaR: the python script works like a charm, but I'd suggest using

emacsclient -e '(let ((last-nonmenu-event nil))(save-buffers-kill-emacs))'
as command. Setting last-nonmenu-event to nil forces emacs into mouse-mode, so you get "nice" dialog boxes instead of prompts in the minibuffer.

Or even more fancy (somewhere in your emacs config):

(defun shutdown-emacs-server () (interactive)
  (when (not (eq window-system 'x))
    (message "Initializing x windows system.")
    (x-initialize-window-system)
    (when (not x-display-name) (setq x-display-name (getenv "DISPLAY")))
    (select-frame (make-frame-on-display display '((window-system . x))))
  )
  (let ((last-nonmenu-event nil)(window-system "x"))(save-buffers-kill-emacs)))

and then:

emacsclient -e '(shutdown-emacs-server)'