Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Last active August 4, 2025 17:45
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."
[f]
(.addShutdownHook
(Runtime/getRuntime)
(new 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