Struct std::process::Command
[−]
[src]
pub struct Command { // some fields omitted }
The Command
type acts as a process builder, providing fine-grained control
over how a new process should be spawned. A default configuration can be
generated using Command::new(program)
, where program
gives a path to the
program to be executed. Additional builder methods allow the configuration
to be changed (for example, by adding arguments) prior to spawning:
use std::process::Command; let output = Command::new("sh") .arg("-c") .arg("echo hello") .output() .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); let hello = output.stdout;
Methods
impl Command
fn new<S: AsRef<OsStr>>(program: S) -> Command
Constructs a new Command
for launching the program at
path program
, with the following default configuration:
- No arguments to the program
- Inherit the current process's environment
- Inherit the current process's working directory
- Inherit stdin/stdout/stderr for
spawn
orstatus
, but create pipes foroutput
Builder methods are provided to change these defaults and otherwise configure the process.
fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command
Add an argument to pass to the program.
fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command
Add multiple arguments to pass to the program.
fn env<K, V>(&mut self, key: K, val: V) -> &mut Command where K: AsRef<OsStr>, V: AsRef<OsStr>
Inserts or updates an environment variable mapping.
Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.
fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command
Removes an environment variable mapping.
fn env_clear(&mut self) -> &mut Command
Clears the entire environment map for the child process.
fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command
Sets the working directory for the child process.
fn stdin(&mut self, cfg: Stdio) -> &mut Command
Configuration for the child process's stdin handle (file descriptor 0).
fn stdout(&mut self, cfg: Stdio) -> &mut Command
Configuration for the child process's stdout handle (file descriptor 1).
fn stderr(&mut self, cfg: Stdio) -> &mut Command
Configuration for the child process's stderr handle (file descriptor 2).
fn spawn(&mut self) -> Result<Child>
Executes the command as a child process, returning a handle to it.
By default, stdin, stdout and stderr are inherited from the parent.
fn output(&mut self) -> Result<Output>
Executes the command as a child process, waiting for it to finish and collecting all of its output.
By default, stdin, stdout and stderr are captured (and used to provide the resulting output).
Examples
fn main() { use std::process::Command; let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); println!("status: {}", output.status); println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); println!("stderr: {}", String::from_utf8_lossy(&output.stderr)); }use std::process::Command; let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); println!("status: {}", output.status); println!("stdout: {}", String::from_utf8_lossy(&output.stdout)); println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
fn status(&mut self) -> Result<ExitStatus>
Executes a command as a child process, waiting for it to finish and collecting its exit status.
By default, stdin, stdout and stderr are inherited from the parent.
Examples
fn main() { use std::process::Command; let status = Command::new("ls").status().unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); println!("process exited with: {}", status); }use std::process::Command; let status = Command::new("ls").status().unwrap_or_else(|e| { panic!("failed to execute process: {}", e) }); println!("process exited with: {}", status);