Skip to content

Commit

Permalink
Add shutdown hook to destroy the process
Browse files Browse the repository at this point in the history
  • Loading branch information
hamann committed Sep 28, 2020
1 parent 0aefe6d commit 9ed1b0d
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/babashka/process.clj
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@
:env
:timeout
:throw
:wait]
:wait
:keep-running-on-shutdown?]
:or {out :string
err :string
dir (System/getProperty "user.dir")
throw true
wait true}}]
wait true
keep-running-on-shutdown? false}}]
(let [in (or in (:out prev))
args (mapv str args)
pb (cond-> (ProcessBuilder. ^java.util.List args)
Expand All @@ -47,6 +49,9 @@
(identical? out :inherit) (.redirectOutput ProcessBuilder$Redirect/INHERIT)
(identical? in :inherit) (.redirectInput ProcessBuilder$Redirect/INHERIT))
proc (.start pb)]
(when-not keep-running-on-shutdown?
(-> (Runtime/getRuntime)
(.addShutdownHook (Thread. #(.destroy proc)))))
(when (string? in)
(with-open [w (io/writer (.getOutputStream proc))]
(binding [*out* w]
Expand Down Expand Up @@ -109,6 +114,6 @@
(let [is (-> (process ["ls"]) :out)]
(process ["cat"] {:in is
:out :inherit})
nil)
nil))


)

0 comments on commit 9ed1b0d

Please sign in to comment.