38

Is there a way to periodically run an elisp function in a long-running emacs, similar to cron, but within the emacs process?

For example I want to "automatically run (recentf-save-list) every half hour" because it otherwise only runs on exit, which sucks when emacs occasionally crashes. (There are other examples as well so looking for a general solution rather than one in particular for recentf).

Jonathan Swartz
  • 1,913
  • 2
  • 17
  • 28

2 Answers2

54

Check out run-with-timer.

(run-with-timer 0 (* 30 60) 'recentf-save-list)
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
  • That link is to the idle timers page (also useful of course). – phils Oct 01 '10 at 22:54
  • 3
    @JSpen The question was to have something run every half hour, and the documentation for `run-with-timer` accepts a number in seconds, so `(* 30 60)` is just a way of denoting 30 minutes. `(* 30 60)` is the lisp way of multiplying 30 and 60. – Trey Jackson Jun 12 '12 at 16:48
  • @TreyJackson - I salute you. You are helpful and patient. – Cheeso Nov 21 '13 at 01:34
4

You might also find midnight mode useful. One can arbitrarily define 'midnight' and then add hooks as desired.

dat
  • 1,580
  • 1
  • 21
  • 30