diff --git a/API.md b/API.md index 1079887..359a69b 100644 --- a/API.md +++ b/API.md @@ -205,10 +205,10 @@ Creates a child process. Takes a command (vector of strings or map of process info just before the process is started. Can be useful for debugging or reporting. Any return value from the function is discarded. Map contents: - `:cmd` - a vector of the tokens of the command to be executed (e.g. `["ls" "foo"]`) - - `:shutdown`: shutdown hook, defaults to `nil`. Takes process - map. Typically used with [`destroy`](#babashka.process/destroy) or [`destroy-tree`](#babashka.process/destroy-tree) to ensure long - running processes are cleaned up on shutdown. The shutdown hook is - executed as soon as the child process ends. + - `:shutdown`: optional JVM shutdown hook. Receives the process map. + Defaults to `nil`; use [`destroy`](#babashka.process/destroy) or [`destroy-tree`](#babashka.process/destroy-tree) to stop the child on shutdown. + On JDK 9+, the hook also runs when the child exits. + See [Choosing a shutdown hook](/README.md#choosing-a-shutdown-hook). - `:exit-fn`: a function which is executed upon exit. Receives process map as argument. Only supported in JDK11+.
diff --git a/README.md b/README.md index 9d0b90b..58647f0 100644 --- a/README.md +++ b/README.md @@ -321,6 +321,28 @@ stream of numbers, line by line and print it ourselves: (p/destroy-tree number-stream) ``` +### Choosing a shutdown hook + +`process` defaults to `:shutdown nil`. A JVM shutdown hook retains the +callback and the process it captures until the hook is removed or the JVM +exits. Registering one for every short-lived child can keep many process +objects alive. Opt in when the child should stop with your application: + +``` clojure +(require '[babashka.process :as p]) + +(let [child (p/process {:shutdown p/destroy} "java" "-version")] + (try + (p/check child) + (finally (p/destroy child)))) +``` + +Use `destroy-tree` to stop descendants as well on JDK 9 or later. For a +child started with `process` on those JDKs, child exit also removes the +hook and starts its callback. On JDK 8 the hook stays registered until JVM +shutdown. Hooks set on individual process builders in a multi-builder +`pipeline` likewise stay registered until shutdown. + ## Printing command The `:pre-start-fn` option can be used to report commands being run: diff --git a/src/babashka/process.cljc b/src/babashka/process.cljc index d5f121c..286e9a8 100644 --- a/src/babashka/process.cljc +++ b/src/babashka/process.cljc @@ -497,10 +497,10 @@ map of process info just before the process is started. Can be useful for debugging or reporting. Any return value from the function is discarded. Map contents: - `:cmd` - a vector of the tokens of the command to be executed (e.g. `[\"ls\" \"foo\"]`) - - `:shutdown`: shutdown hook, defaults to `nil`. Takes process - map. Typically used with `destroy` or `destroy-tree` to ensure long - running processes are cleaned up on shutdown. The shutdown hook is - executed as soon as the child process ends. + - `:shutdown`: optional JVM shutdown hook. Receives the process map. + Defaults to `nil`; use `destroy` or `destroy-tree` to stop the child on shutdown. + On JDK 9+, the hook also runs when the child exits. + See [Choosing a shutdown hook](/README.md#choosing-a-shutdown-hook). - `:exit-fn`: a function which is executed upon exit. Receives process map as argument. Only supported in JDK11+." {:arglists '([opts? & args])} [& args]