Skip to main content

Crate subprocess

Crate subprocess 

Source
Expand description

Execution of and interaction with external processes and pipelines.

The main entry points to the crate are the Exec and Pipeline builders. They provide a builder-pattern API with convenient methods for streaming and capturing of output, as well as combining commands into pipelines.

Compared to std::process, the crate provides these additional features:

  • Connecting multiple commands into OS-level pipelines.

  • The capture and communicate family of methods for deadlock-free capturing of subprocess output/error, while simultaneously feeding data to its standard input. Capturing supports optional timeout and read size limit.

  • Flexible redirection options, such as connecting standard streams to arbitrary open files, or merging output streams like shell’s 2>&1 and 1>&2 operators.

  • Non-blocking and timeout methods to wait on the process: poll, wait, and wait_timeout.

§Examples

Execute a command and capture its output:

let out = Exec::cmd("echo").arg("hello").capture()?.stdout_str();
assert!(out.contains("hello"));

Use the Exec builder to execute a pipeline of commands and capture the output:

let dir_checksum = {
    Exec::shell("find . -type f") | Exec::cmd("sort") | Exec::cmd("sha1sum")
}.capture()?.stdout_str();

Modules§

unix
Subprocess extensions for Unix platforms.

Structs§

Capture
Data captured by Exec::capture and Pipeline::capture.
Communicator
Send input to a subprocess and capture its output, without deadlock.
Exec
A builder for creating subprocesses.
ExitStatus
Exit status of a process.
InputData
Type-erased readable source for input data fed to a subprocess’s stdin.
Job
Interface to a started process or pipeline.
Pipeline
A builder for pipelines of subprocesses connected via pipes.
Process
A handle to a running or finished subprocess.

Enums§

Redirection
Instruction what to do with a stream in the child process.

Traits§

ExecExt
Extension trait for Unix-specific process creation options.
FromSink
Trait for converting a sink type into an output redirection.
FromSource
Trait for converting a source type into an input redirection.
JobExt
Unix-specific extension methods for Job.
PipelineExt
Unix-specific extension methods for Pipeline.