Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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+.
<p><sub><a href="https://github.com/babashka/process/blob/master/src/babashka/process.cljc#L453-L507">Source</a></sub></p>

Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions src/babashka/process.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading