Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active March 13, 2026 19:18
Show Gist options
  • Select an option

  • Save wdhowe/5477a35ceb89cdadd36be673861f0a65 to your computer and use it in GitHub Desktop.

Select an option

Save wdhowe/5477a35ceb89cdadd36be673861f0a65 to your computer and use it in GitHub Desktop.
Clojure shutdown hook
(ns myapp.shutdown-hook
(:gen-class))
(defn add-shutdown-hook!
"Adds a shutdown hook that executes the given function when the JVM is shutting down.
Parameters:
- f - function to execute on shutdown
Returns nil."
[f]
(.addShutdownHook (Runtime/getRuntime) (Thread. ^Runnable f)))
;; Usage
(defn -main [& _]
(add-shutdown-hook!
(fn []
(println "Shutting down cleanly...")))
(println "Application running. Press Ctrl+C to stop.")
(Thread/sleep 99999999)) ; simulate long-running app
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment