9

Is it possible to execute a function or command at a specific time? As an explicit example, is it possible to kill emacs at a specific time (time determined by the OS).

Drew
  • 75,699
  • 9
  • 109
  • 225
Name
  • 7,689
  • 4
  • 38
  • 84

1 Answers1

18

You want to use the function run-at-time, described in Chapter 38.10 of the Emacs Lisp manual. In your case, something like this should do the trick:

(run-at-time "20:30" nil #'kill-emacs)

If the function you want to call takes parameters, you can specify them as additional parameters to run-at-time:

(run-at-time "5 sec" nil #'message "Tempus volat, hora fugit.")

Note that absolute times are taken to be today, even if they are in the past, so the first example above will exit immediately if it is after 20:30.

jch
  • 5,680
  • 22
  • 39
  • 1
    I put your code it in my .emacs file. It kills emacs at the given time! But when I restart emacs, it is immediately killed. Any hint? – Name Jan 04 '15 at 23:11
  • 1
    @Name edited to clarify. – jch Jan 04 '15 at 23:24
  • LOL, you can start with the -q switch to bypass the init file that'll be toasting your emacs until midnight. Then the glass slipper falls and you can edit again until that same time tomorrow. – Devon Apr 29 '19 at 15:26