I have a problem with emacs started as daemon using Systemd.

Every shutdown/reboot if I don't stop emacs daemon service manually, the history of recent opened files in current session is lost. I think this is because of the Systemd that can't properly kill Emacs when in a shutdown state.

What can I do to solve this problem?

My recentf config in init.el:

(use-package recentf
    :config (progn (setq recentf-auto-cleanup 'never
                         recentf-max-menu-items 50
                         recentf-max-saved-items 400
                         (expand-file-name "temp/.recentf" user-emacs-directory))
                   (recentf-mode t)))

Systemd emacs service config:

Description=Emacs: the extensible, self-documenting text editor

ExecStart=/usr/bin/emacs --daemon
ExecStop=/usr/bin/emacsclient --eval "(kill-emacs)"


  • 1
    Shouldn't you be asking this on a systemd forum? Assuming your use-package code works in other circumstances (?) then it would seem like the only issue is that systemd doesn't run your ExecStop command.
    – phils
    Commented Sep 13, 2015 at 2:53

4 Answers 4


You can save recent files just before shutdown by calling recentf-save-list

M-x recentf-save-list RET

This adds your recent files to recentf history.

If you dont want to do this manually, you can add a timer which will do that for you every 5 minutes.

(run-at-time (current-time) 300 'recentf-save-list)

Add this to your config. So whenever you open emacs, it will call the function and every 5 minutes(or 300 seconds) it will go on calling that function.

Source: https://emacs.stackexchange.com/a/15115/5187


I know this thread is old, but i came across the same problem in MS Windows (i.e. Windows does know how to exit Emacs daemon gracefully). I solved this for me by hooking recentf-save-list into terminal-delete-functions. So in my init file I have the following line:

(add-hook 'delete-terminal-functions (lambda (terminal) (recentf-save-list)))

Then the recentf-list of the current session is saved whenever you close Emacs client. This was enough for me, because I am used to closing Emacs before shutting down my PC.


Another solution is to save the list of recentf files every time that you visit a new file. The below lined added to the init file will do the job:

(add-hook 'find-file-hook 'recentf-save-list)

I know it is too late, but I want to share it with you. This answer is first posted here, to save your time, I copy it there.

I am not quite sure about this answer, so I originally want to post it as a comment, but as you see, I don't have enough reputation to add a comment, so I have to post it as an answer.

Remember that I have made other changes to my .emacs.d, so it is possible that my answer is totally wrong. If it doesn't work for you, you may follow this link.

I have run into similar case as yours. I run emacs as daemon using systemd. And when I poweroff, recentf can't be save, but when I stop daemon manually, it can. Then I found something I ignored here.

To save time, the core is that Emacs built with gtk support is not a good choice when using Emacs as a daemon, because there is some bug.

You can also see Emacs itself say something about this, if you journalctl it:

Emacs might crash when run in daemon mode and the X11 connection is unexpectedly lost.
Using an Emacs configured with --with-x-toolkit=lucid does not have this problem.

So the point is clear now. Don't build your Emacs with gtk when using it as daemon.

For the solution, I think it may be interrelated to your distribution. As for me, on Gentoo, it is to enable useflag "motif" and disable useflag "athena" "gtk" "Xaw3d".

For others distribution, you may find it yourself.

The link I found say this:

USE flags for toolkits gtk, motif, athena, and aqua are mutually exclusive. Generally, USE="gtk" is a good choice. However, if intending to use Emacs as a daemon, USE="motif -athena -gtk -Xaw3d" or USE="athena Xaw3d -gtk -motif" is recommended instead because of bug #292471. USE="athena Xaw3d" resembles USE="gtk" very well. USE="aqua" is a special case applying only to Mac OS X.

  • I think if the reason is right, then we can ask systemd to kill emacs before X quit, so we can still use gtk one. But I don't know how to do this.
    – C-Entropy
    Commented Sep 20, 2020 at 2:35

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.