Creche
Configure, run, and monitor single child processes or entire pipelines of processes. Redirect file descriptors to and from those processes. Control environment variables.
Creche is an alternative to Command and friends in the standard library.
Goals
- Minimal dependencies
- No macros, just regular Rust syntax
- Keep the easy stuff easy, without sacrificing configurability
Limitations
- Linux only
- Doesn't support
nostd - Doesn't handle child process priveleges/capabilities yet
- No async support yet
How to Use
The ChildBuilder and SimplePipelineBuilder types are top level
exports, so many common use cases are enabled with a simple use:
use *;
Read the changelog for a description of breaking changes to the API.
Examples
Read output of cat into a String:
// configure the child process
let mut cmd = new;
cmd.arg
.redirect21; // redirect stderr to stdout
let read_fd = cmd.pipe_from_stdout;
// run it
let mut child = cmd.spawn;
// read stdout from the child process
let f = from;
let output = read_to_string?;
println!;
println!;
Write data to a child process:
let mut cmd = new;
cmd.arg
.arg;
let write_fd = cmd.pipe_to_stdin;
let child = cmd.spawn;
// write some data
let mut f = from;
writeln!;
// don't forget to close your fd. Or at least .flush() the thing.
drop;
println!;
Set up fzf to run in an environment with just PATH, HOME, and FZF_DEFAULT_COMMAND. Then run it and get its output:
// configure the environment
let mut env = new;
env.keep
.set
// Note: $HOME is expanded in the shell spawned by fzf.
// It's not creche magic.
.set;
// configure the child process
let mut cmd = new;
cmd.arg
.arg
.set_env;
let read_fd = cmd.pipe_from_stdout;
// run it
let child = cmd.spawn;
// read the result
let mut f = from;
let output = read_to_string?;
println!;
println!;
Pipe the output of ls into sort into tr. Redirect stderr for all of that to /dev/null:
let mut ls_cmd = new;
ls_cmd.arg;
let mut sort_cmd = new;
sort_cmd.arg;
let mut tr_cmd = new;
tr_cmd.arg;
tr_cmd.arg;
let mut pipeline = new;
let mut children = pipeline
.add_builder
.add_builder
.add_builder
.quiet
.spawn;
println!;
Block on .wait()ing a child process and send a SIGTERM from another thread:
// sleep for a few seconds
let mut cmd = new;
cmd.arg;
println!;
let child = cmd.spawn;
// get a "handle" to the child process so that we may signal it
let handle = child.get_handle;
// start a thread that sends SIGTERM after a short pause
spawn;
// collect the child exit status
println!;