I am using emacs 24.3 (ubuntu 14.10) and would like the daemon to participate in the graphical desktop session lifecycle while using desktop-mode. I went through http://www.emacswiki.org/emacs/EmacsAsDaemon but it still does not play nice for me. One problem is that the daemon SIGTERM termination does not clean up .emacs.desktop.lock so the next start will refuse to pick up the saved state.

Does anyone have a recipe to make emacs daemon + desktop mode play nice with the graphical desktop session?

As a sidenote: Sending SIGHUP frequently crashes emacs for me.

2 Answers 2


Don't kill the process.

Use M-x kill-emacs RET to terminate the server.

  • Ok, that cleans up the look. I may be missing something, but how does one execute that termination of the graphical session? The easiest thing I could think of would be a custom OS signal handler. Commented Feb 11, 2015 at 10:13
  • 2
    If you have an active client frame, you can issue kill-emacs directly. If you have no current clients, you can run emacsclient -e '(kill-emacs)'. If you just want Emacs to shut down more gracefully when killed by a signal, you'll probably need to M-x report-emacs-bug (after checking to see whether it's already been logged).
    – phils
    Commented Feb 11, 2015 at 23:32
  • Sure. The thing is that I do not want to bother and manually tell every program to shut down gracefully. Desktop session managers do tell programs the session is about to terminate. I want emacs to automatically take action at that time. Commented Feb 12, 2015 at 8:19
  • Well check the bug reports, report a new one if necessary, and once you have more information, update the question, or post an answer.
    – phils
    Commented Feb 12, 2015 at 10:26

This is how I solved it for me: (you need to disable elsewhere desktop-save-mode)

(require 'desktop)
;; (setq desktop-restore-forces-onscreen nil)

(if (not (daemonp))
    (desktop-save-mode 1)
  (defun restore-desktop (frame)
    "Restores desktop and cancels hook after first frame opens. 
     So the daemon can run at startup and it'll still work"
    (with-selected-frame frame
      (desktop-save-mode 1)
      (remove-hook 'after-make-frame-functions 'restore-desktop)))
  (add-hook 'after-make-frame-functions 'restore-desktop))

Uncomment first line if you get the Error (frameset): Wrong type argument: number-or-marker-p, nil.

Btw, use (global-set-key (kbd "C-x C-M-c") 'save-buffers-kill-emacs) to leave emacsclient safely.

  • Thats not enabling it if it starts via emacsclient though is it?
    – RichieHH
    Commented Nov 13, 2023 at 22:47

Your Answer

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

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