0

Problem

I have run the following Clojure code on both my Ubuntu development machine and the BeagleBone Black, and can confirm that it works on the former but not the latter.

(defn setup-shutdown-hook!                           
  [f]                                                  
  (.addShutdownHook (Runtime/getRuntime) (Thread. f))) 

(setup-shutdown-hook!
  (fn []
    (println "I am no more")))

(defn -main []
  (doseq [i (range)]
    (Thread/sleep 1000)
    (println "Staying alive")))

On the BBB, the shutdown hook does work appropriately in the case of "natural" shutdowns, but not on Ctrl-C. On the Ubuntu machine, it seems to always work.

Is there some explanation for this different behavior? Java versions? System level differences? Is there a workaround for getting the desired behavior?

System details

Ubuntu:

java version "1.7.0_65"
OpenJDK Runtime Environment (IcedTea 2.5.2) (7u65-2.5.2-3~14.04)
OpenJDK 64-Bit Server VM (build 24.65-b04, mixed mode)

Beaglebone (running Angstrom; connected via SSH):

> java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) Client VM (build 24.60-b09, mixed mode)
> uname -a
Linux beaglebone 3.8.13 #1 SMP Wed Sep 4 09:09:32 CEST 2013 armv7l GNU/Linux

P.S. For those of you familiar with Java but not Clojure, the Clojure code above just hooks into the Java methods through it's interop. Hopefully you can test the trans

metasoarous
  • 2,854
  • 1
  • 23
  • 24
  • Missing information: what distribution (including version) are you running on your bbb? How are you connecting to it in order to run your program? – unixsmurf Oct 27 '14 at 07:56
  • Added above; thanks for the question. I'm connected via SSH, and running via leiningen. – metasoarous Oct 27 '14 at 17:29

1 Answers1

1

Oh dear... there's no mystery here. I was lazy, cheated a bit, and ran on my Ubuntu machine with lein exec instead of lein run (as I did on the BBB) ._. Apparently, things seem to behave differently this way...

More to the point of how to get the desired behavior, lein trampoline run -m <ns> does the trick. The problem appears to be that since using lein run effectively runs your application within a nested JVM, the leiningen process catches the Ctrl-C instead of your application code. Using lein trampoline detaches the application code JVM such that it catches the Ctrl-C, and runs the shutdown code as desired.

Thanks to this post for showing me the light, and Stack overflow for it's wonderful "Related" questions.

Community
  • 1
  • 1
metasoarous
  • 2,854
  • 1
  • 23
  • 24