Module std::process1.0.0 [] [src]

A module for working with processes.

This module is mostly concerned with spawning and interacting with child processes, but it also provides abort and exit for terminating the current process.

Spawning a process

The Command struct is used to configure and spawn processes:

use std::process::Command;

let output = Command::new("echo")
                     .arg("Hello world")
                     .expect("Failed to execute command");

assert_eq!(b"Hello world\n", output.stdout.as_slice());Run

Several methods on Command, such as spawn or output, can be used to spawn a process. In particular, output spawns the child process and waits until the process terminates, while spawn will return a Child that represents the spawned child process.

Handling I/O

The stdout, stdin, and stderr of a child process can be configured by passing an Stdio to the corresponding method on Command. Once spawned, they can be accessed from the Child. For example, piping output from one command into another command can be done like so:

use std::process::{Command, Stdio};

// stdout must be configured with `Stdio::piped` in order to use
// `echo_child.stdout`
let echo_child = Command::new("echo")
    .arg("Oh no, a tpyo!")
    .expect("Failed to start echo process");

// Note that `echo_child` is moved here, but we won't be needing
// `echo_child` anymore
let echo_out = echo_child.stdout.expect("Failed to open echo stdout");

let mut sed_child = Command::new("sed")
    .expect("Failed to start sed process");

let output = sed_child.wait_with_output().expect("Failed to wait on sed");
assert_eq!(b"Oh no, a typo!\n", output.stdout.as_slice());Run

Note that ChildStderr and ChildStdout implement Read and ChildStdin implements Write:

use std::process::{Command, Stdio};
use std::io::Write;

let mut child = Command::new("/bin/cat")
    .expect("failed to execute child");

    // limited borrow of stdin
    let stdin = child.stdin.as_mut().expect("failed to get stdin");
    stdin.write_all(b"test").expect("failed to write to stdin");

let output = child
    .expect("failed to wait on child");

assert_eq!(b"test", output.stdout.as_slice());Run



Representation of a running or exited child process.


A handle to a child process's stderr.


A handle to a child process's standard input (stdin).


A handle to a child process's standard output (stdout).


A process builder, providing fine-grained control over how a new process should be spawned.


Describes the result of a process after it has terminated.


The output of a finished process.


Describes what to do with a standard I/O stream for a child process when passed to the stdin, stdout, and stderr methods of Command.



Terminates the process in an abnormal fashion.


Terminates the current process with the specified exit code.

id [

Returns the OS-assigned process identifier associated with this process.