exit-hooks provide a portable way to automatically call some user defined function when exit Common Lisp (Both (quit)
from REPL or kill in shell). Like atexit
in C and Python or Java’s Runtime.addShutdownHook()
. It currently support SBCL, CCL, ECL, ABCL, Allegro CL, clisp and CMUCL. Before exit-hooks, there is no portable way to doing so and no staightforward way to use an exit hook on ABCL. It can be used for tasks like parmenantly save something when exit Lisp.
(ql:quickload :exit-hooks)
To add a function FUNC called when Common Lisp exits:
(exit-hooks:add-exit-hook #'func)
The function FUNC must be of zero arguments. If several functions are added this way, their calling sequence is not specified. If you want to call them in a specific sequence, you can simply wrap them into a single function and add it.
To visit current exit hooks or delete one, you can visit or delete elements from variable exit-hooks:*exit-hooks*
, it’s a list contains current exit hooks. But you should not add exit hooks by adding elements to *exit-hooks*
, use add-exit-hook
only, because elements in *exit-hooks*
is implementation specific.
Thanks to Vibhu Mohindra from ABCL mail list and R. Matthew Emerson from OpenMCL mail list. They provide methods for exit-hooks for ABCL and CCL. And Jocelyn Fréchot gives many useful suggestions and support for clisp, Allegro CL and CMUCL.
For some implementation, for example SBCL, when you are using SLIME, you need to exit Lisp in *inferior-lisp*
, not *slime-repl*
. If you directly run Lisp from shell, not in SLIME, there is no problem. Currently exit-hooks support most of popular Lisp implementations, but not all. If your implementation is not supported and you know how to do that, please tell me. Your work maybe helpful to other fellow Lispers. And this Library only provide a calling function when exit Lisp. Previous version of exit-hooks provide ability to add exit hooks with calling priority, but this can be easily done by wrap several functions into a single one and most people only want a portable layer so this is removed. If you want something to deal all kinds of OS signal handling capability you may have a look at trivial-signal.