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).

2
  • 1
    See also midnight-mode
    – nanny
    Commented Jan 5, 2015 at 14:00
  • I see someone is taking hard measure to prevent emacs addiction :) Commented Apr 22, 2016 at 14:17

1 Answer 1

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.

3
  • 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
    Commented Jan 4, 2015 at 23:11
  • 1
    @Name edited to clarify.
    – jch
    Commented Jan 4, 2015 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
    Commented Apr 29, 2019 at 15:26

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.