I'm using babashka.process/process in bb and it seems like neither the :shutdown or :exit-fn hooks are being fired. Am I doing something wrong?
(p/process {:out :write
:out-file "output.txt"
:pre-start-fn (fn [args] (log/info (str "start: " args)))
:exit-fn (fn [args] (log/info (str "exit: " args)))
:shutdown (fn [args] (log/info (str "shutdown:" args)))}
"./fake_worker.sh")
results in
2025-04-21T10:23:09.516Z mahogany INFO [get-weather:3] - start: {:cmd ["./fake_worker.sh"]}
{:proc #object[java.lang.ProcessImpl 0x193c71fd "Process[pid=1336523, exitValue=\"not exited\"]"],
:exit nil,
:in
#object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x751761a5 "java.lang.ProcessImpl$ProcessPipeOutputStream@751761a5"],
:out #object[java.lang.ProcessBuilder$NullInputStream 0x30d7390e "java.lang.ProcessBuilder$NullInputStream@30d7390e"],
:err
#object[java.lang.ProcessImpl$ProcessPipeInputStream 0xa48311a "java.lang.ProcessImpl$ProcessPipeInputStream@a48311a"],
:prev nil,
:cmd ["./fake_worker.sh"]}
$> (deref *1)
{:proc #object[java.lang.ProcessImpl 0x193c71fd "Process[pid=1336523, exitValue=0]"],
:exit 0,
:in
#object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x751761a5 "java.lang.ProcessImpl$ProcessPipeOutputStream@751761a5"],
:out #object[java.lang.ProcessBuilder$NullInputStream 0x30d7390e "java.lang.ProcessBuilder$NullInputStream@30d7390e"],
:err
#object[java.lang.ProcessImpl$ProcessPipeInputStream 0xa48311a "java.lang.ProcessImpl$ProcessPipeInputStream@a48311a"],
:prev nil,
:cmd ["./fake_worker.sh"]}
$>
Do you have the fake-worker script as well so I can repro?
When I run this I see both :exit and :shutdown being printed:
(def my-proc (p/process {:out :write
:out-file "output.txt"
:pre-start-fn (fn [args] (prn :args args))
:exit-fn (fn [args] (prn :exit))
:shutdown (fn [args] (prn :shutdown))}
"ls"))
(prn @my-proc)$ bb /tmp/repro_process.clj
:args {:cmd ["ls"]}
:exit
:shutdownThis is fake_worker.sh
#!/usr/bin/env bash
echo "\"working...\""
sleep 5
echo "oh crap!, quitting"
so it's nothing special, I thinkok... one difference, I'm running my script in a cider repl...
$ bb /tmp/repro_process.clj
:args {:cmd ["/tmp/fake_worker.sh"]}
:exit
:shutdown
{:proc #object[java.lang.ProcessImpl 0x97e47b4 "Process[pid=76351, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessImpl$ProcessPipeOutputStream 0x3f1c4aa1 "java.lang.ProcessImpl$ProcessPipeOutputStream@3f1c4aa1"], :out #object[java.lang.ProcessBuilder$NullInputStream 0x794026ae "java.lang.ProcessBuilder$NullInputStream@794026ae"], :err #object[java.lang.ProcessImpl$ProcessPipeInputStream 0x2042025f "java.lang.ProcessImpl$ProcessPipeInputStream@2042025f"], :prev nil, :cmd ["/tmp/fake_worker.sh"]}Perhaps you're just not seeing the output because it's logged elsewhere?
in your console rather than in the REPL or so
The log/info from :pre-start-fn comes to the repl though?
try a normal script first without the REPL
that seems to make the difference. with $ bb test.clj I see it being printed...
can you try a non-I/O side effect in the exit+shutdown functions to see if they are being executed in the REPL? if so, I would assume it's something weird with REPL I/O and threads perhaps
you might be right... I tried with an atom and did a (swap! a inc) in the hooks, and the counter incremented twice...
what you can also do, if you prefer, to use the normal Clojure JVM REPL tooling and then run your script with bb occasionally to see if it still works.
with bb print-deps you get a deps.edn that you can use with a regular REPL
thanks for helping 🙂
I do see that the bb.process dependency printed in the bb print-deps output is a bit behind... :-) So I think you probably want to manually bump it. I'll bump it now
https://github.com/babashka/babashka/commit/6c0cfde92f66db397dc6342646a2d971332f62b9