20

I have the following code:

(ns test-hook.core)

(defn -main []
  (.addShutdownHook (Runtime/getRuntime) (Thread. #(println "shutdown")))
  (println "start")
  (doseq [i (range 1 6)]
    (Thread/sleep 1000)
    (println i)))

and the following project.clj

(defproject test-hook "1.0.0-SNAPSHOT"
  :aot :all
  :main test-hook.core
  :description "FIXME: write description"
  :dependencies [[org.clojure/clojure "1.2.0"]])

when I run it with "lein run" the shutdown hook only gets executed on normal program execution, not when receiving SIGINT (Ctrl-C)

the same code when ran outside of lein successfully executes the shutdown hook even when receiving SIGINT.

how can I have the shutdown hook executed when running from lein and aborting with Ctrl-C?

Samus_
  • 2,903
  • 1
  • 23
  • 22

1 Answers1

32

Have you tried running it with trampoline?

lein trampoline run

Seems to work for me.

AFAIK "lein trampoline" doesn't nest the JVM, so your Ctrl-C isn't caught by leiningen, but by your code.

wink
  • 466
  • 4
  • 7
  • 12
    Worked for me too. Anyone know *why* trampoline is really required? Even if the JVMs are "nested" Leiningen still kills the app's JVM, which should trigger its shutdown hooks, unless it's a SIGKILL or something. Looking at lein source it appears to use `Process#destroy()` to end the nested JVM, which appears to use SIGTERM... I'm confused. – overthink Nov 25 '14 at 04:24