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

| improve this answer | |

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.

| improve this answer | |

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)
| improve this answer | |

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.