As noted in the 4th example, sh
uses futures. This means that if your program uses sh
and then finishes its execution it will unexpectedly hang and not terminate/exit. The sh
future will still be alive in the background and will be holding up the program.
This is a bit confusing when you first try to use Clojure for scripting as it looks like your script doesn't exit naturally. Furthermore, when you run sh
in the REPL the background futures aren't apparent to the user and everything works as-expected
To fix the situation you can either run (System/exit 0)
to terminate your program explicitly. Or you can run (shutdown-agents)
to kill the background future and then the program will exit naturally
For a discussion of this strange behavior see: https://clojureverse.org/t/why-doesnt-my-program-exit/3754/2