Crate subprocess [−] [src]
Execution and interaction with external processes.
The module has two entry points. One is the Popen
struct,
inspired by Python's subprocess.Popen
. This is useful when a
greater amount of control is needed, or when porting Python code
written for Python's subprocess
. The other entry point is the
Exec
struct written in the builder style more native to Rust,
similar to std::process::Command
.
Examples
Create Popen
directly in order to communicate with a process and
optionally terminate it:
let mut p = Popen::create(&["ps", "x"], PopenConfig { stdout: Redirection::Pipe, ..Default::default() })?; // Since we requested stdout to be redirected to a pipe, the parent's // end of the pipe is available as p.stdout. It can either be read // directly, or processed using the communicate() method: let (out, err) = p.communicate(None)?; // check if the process is still alive if let Some(exit_status) = p.poll() { // the process has finished } else { // it is still running, terminate it p.terminate()?; }
Use the Exec
builder to execute a command and capture its
output:
let output = Exec::cmd("command").arg("arg1").arg("arg2") .stdout(Redirection::Pipe) .capture()? .stdout_str();
Structs
Exec |
A builder for |
NullFile |
Marker value for |
Pipeline |
A builder for multiple |
Popen |
Interface to a running subprocess. |
PopenConfig |
Structure designed to be passed to |
Enums
ExitStatus |
Exit status of a process. |
PopenError |
Error in |
Redirection |
Instruction what to do with a stream in the child process. |
Type Definitions
Result |
Result type for the |