process
function
babashka.process/process
(process [opts? & args])Creates a child process. Takes a command (vector of strings or
objects that will be turned into strings) and optionally a map of
options.
Returns: a record with:
- `:proc`: an instance of `java.lang.Process`
- `:in`, `:err`, `:out`: the process's streams. To obtain a string from
`:out` or `:err` you will typically use `slurp` or use the `:string`
option (see below). Slurping those streams will block the current thread
until the process is finished.
- `:cmd`: the command that was passed to create the process.
- `:prev`: previous process record in case of a pipeline.
The returned record can be passed to `deref`. Doing so will cause the current
thread to block until the process is finished and will populate `:exit` with
the exit code.
Supported options:
- `:cmd`: a vector of strings. A single string can be tokenized into a vector of strings with `tokenize`.
Overrides the variadic `args` argument.
- `:in`, `:out`, `:err`: objects compatible with `clojure.java.io/copy` that
will be copied to from the process's corresponding stream.
May be set to `:inherit` for redirecting to the parent process's corresponding
stream. Optional `:in-enc`, `:out-enc` and `:err-enc` values will
be passed along to `clojure.java.io/copy`.
For redirecting to Clojure's `*in*`, `*out*` or `*err*` stream, set
the corresponding option accordingly.
The `:out` and `:err` options support `:string` for writing to a string
output and `:bytes` for writing to a byte array. You will need to `deref`
the process before accessing the string or byte array via the process's `:out`.
To redirect `:err` to `:out`, specify `:err :out`.
For writing output to a file, you can set `:out` and `:err` to a `java.io.File` object, or a keyword:
- `:write` + an additional `:out-file`/`:err-file` + file to write to the file.
- `:append` + an additional `:out-file`/`:err-file` + file to append to the file.
To discard `:out` or `:err`, use `:discard`
- `:prev`: output from `:prev` will be piped to the input of this process. Overrides `:in`.
- `:inherit`: if true, sets `:in`, `:out` and `:err` to `:inherit`.
- `:dir`: working directory.
- `:env`, `:extra-env`: a map of environment variables. See [Add environment](/README.md#add-environment).
- `:escape`: function that will applied to each stringified argument. On
Windows this defaults to prepending a backslash before a double quote. On
other operating systems it defaults to `identity`.
- `:pre-start-fn`: a one-argument function that, if present, gets called with a
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.
- `:exit-fn`: a function which is executed upon exit. Receives process map as argument. Only supported in JDK11+.
Examples
No examples yet. Be the first to add one!