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 Sep 13 '15 at 2:53

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.


After quite some fiddling with the same issue, I came about the following setup:

(use-package recentf
  :defer t
  ;; Better than saving every five minutes is to do it every time a file is
  ;; visited.
  (find-file . my/silently-recentf-save-list)
  ;; Adapted from https://emacs.stackexchange.com/a/45700/18951
  (defun my/silently-recentf-save-list ()
    "Save recent files list silently."
    (let ((save-silently t))
  (recentf-mode t))

I settled for this setup after checking recentf.el in which we find the following hooks used by it:

(defconst recentf-used-hooks
    (find-file-hook       recentf-track-opened-file)
    (write-file-functions recentf-track-opened-file)
    (kill-buffer-hook     recentf-track-closed-file)
    (kill-emacs-hook      recentf-save-list)
  "Hooks used by recentf.")

Which means recentf is keeping track of changes in the recent files list at find-file-hook, write-file-functions and kill-buffer-hook. Adding a save action to the last two hooks somehow induced an infinite loop of saving recentf, so I left those out. I think it is enough to save every time I visit a file, and somewhat better than simply saving it every 5 minutes which seems to be the general use for the purpose.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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