From 068d5302d6e3e2db8d2290946f50caa063de0744 Mon Sep 17 00:00:00 2001 From: Holger Amann Date: Mon, 28 Sep 2020 11:36:48 +0200 Subject: [PATCH] Add shutdown hook to destroy the process --- src/babashka/process.clj | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/babashka/process.clj b/src/babashka/process.clj index 1008f5f..4207e27 100644 --- a/src/babashka/process.clj +++ b/src/babashka/process.clj @@ -32,12 +32,14 @@ :env :timeout :throw - :wait] + :wait + :destroy-on-shutdown] :or {out :string err :string dir (System/getProperty "user.dir") throw true - wait true}}] + wait true + destroy-on-shutdown true}}] (let [in (or in (:out prev)) args (mapv str args) pb (cond-> (ProcessBuilder. ^java.util.List args) @@ -47,6 +49,9 @@ (identical? out :inherit) (.redirectOutput ProcessBuilder$Redirect/INHERIT) (identical? in :inherit) (.redirectInput ProcessBuilder$Redirect/INHERIT)) proc (.start pb)] + (when destroy-on-shutdown + (-> (Runtime/getRuntime) + (.addShutdownHook (Thread. #(.destroy proc))))) (when (string? in) (with-open [w (io/writer (.getOutputStream proc))] (binding [*out* w] @@ -109,6 +114,6 @@ (let [is (-> (process ["ls"]) :out)] (process ["cat"] {:in is :out :inherit}) - nil) + nil)) + - )