forkbridge
is a small, well-tested Clojure library for spawning and interacting with subprocesses.
It provides a simple interface over Java's ProcessBuilder
that lets you:
The API is minimal, transparent, and structured as a functional map with closures.
(require '[forkbridge.core :refer [start-process]])
(def p (start-process ["clojure"]))
((:write-line! p) "(+ 1 2)")
(println ((:read-line-stdout! p))) ; => "user=> 3"
((:write-line! p) "(System/exit 0)")
((:wait! p))
(println ((:exit-value p))) ; => 0
Each call to start-process
returns a map with the following keys:
Key | Description |
---|---|
:alive? | Returns true if the process is still running |
:exit-value | Returns the exit code (or nil if the process hasn't exited yet) |
:write-line! | Writes a line to the process's stdin (with a newline); returns true if successful, nil if the process is dead |
:read-line-stdout! | Blocks until a full line is available from stdout; returns the line, or nil if the process is dead |
:read-line-stderr! | Blocks until a full line is available from stderr; returns the line, or nil if the process is dead |
:sigterm! | Sends SIGTERM to request graceful termination (non-blocking) |
:sigkill! | Sends SIGKILL to forcibly terminate the process (non-blocking) |
:wait! | Blocks until the process exits |
Each of these is a function or thunk — to use them, call them like so:
((:write-line! p) "(+ 2 2)")
((:read-line-stdout! p)) ; => "user=> 4"
The test suite includes:
:alive?
, :exit-value
)stdout
/stderr
)See forkbridge.core-test
for working examples.
This library favours:
While babashka.process
is a powerful and flexible library for managing subprocesses, forkbridge
offers a different tradeoff, with a focus on minimalism.
In the future, we may add some more features to forkbridge
. We're still coming up with new ideas, but for now this is a usable base that just gets out of the way and provides some safety cushioning around interacting with subprocesses.
forkbridge
:sigterm!
, :sigkill!
, :read-line-stdout!
, etc. matches shell behavior clearly and transparently.babashka.process
insteadls | grep foo
).:inherit
, :string
, or background execution).MIT Licence. See LICENCE for details.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close