Struct std::io::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::io::Command; let mut process = match Command::new("sh").arg("-c").arg("echo hello").spawn() { Ok(p) => p, Err(e) => fail!("failed to execute process: {}", e), }; let output = process.stdout.get_mut_ref().read_to_end();
Methods
impl Command
fn new<T: ToCStr>(program: T) -> 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
- A readable pipe for stdin (file descriptor 0)
- A writeable pipe for stdout and stderr (file descriptors 1 and 2)
Builder methods are provided to change these defaults and otherwise configure the process.
fn arg<'a, T: ToCStr>(&'a mut self, arg: T) -> &'a mut Command
Add an argument to pass to the program.
fn args<'a, T: ToCStr>(&'a mut self, args: &[T]) -> &'a mut Command
Add multiple arguments to pass to the program.
fn env<'a, T: ToCStr>(&'a mut self, env: &[(T, T)]) -> &'a mut Command
Sets the environment for the child process (rather than inheriting it from the current process).
fn cwd<'a>(&'a mut self, dir: &Path) -> &'a mut Command
Set the working directory for the child process.
fn stdin<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stdin handle (file descriptor 0).
Defaults to CreatePipe(true, false) so the input can be written to.
fn stdout<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stdout handle (file descriptor 1).
Defaults to CreatePipe(false, true) so the output can be collected.
fn stderr<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Configuration for the child process's stderr handle (file descriptor 2).
Defaults to CreatePipe(false, true) so the output can be collected.
fn extra_io<'a>(&'a mut self, cfg: StdioContainer) -> &'a mut Command
Attaches a stream/file descriptor/pipe to the child process. Inherited
file descriptors are numbered consecutively, starting at 3; the first
three file descriptors (stdin/stdout/stderr) are configured with the
stdin, stdout, and stderr methods.
fn uid<'a>(&'a mut self, id: uint) -> &'a mut Command
Sets the child process's user id. This translates to a setuid call in
the child process. Setting this value on windows will cause the spawn to
fail. Failure in the setuid call on unix will also cause the spawn to
fail.
fn gid<'a>(&'a mut self, id: uint) -> &'a mut Command
Similar to uid, but sets the group id of the child process. This has
the same semantics as the uid field.
fn detached<'a>(&'a mut self) -> &'a mut Command
Sets the child process to be spawned in a detached state. On unix, this means that the child is the leader of a new process group.
fn spawn(&self) -> IoResult<Process>
Executes the command as a child process, which is returned.
fn output(&self) -> IoResult<ProcessOutput>
Executes the command as a child process, waiting for it to finish and collecting all of its output.
Example
fn main() { use std::io::Command; use std::str; let output = match Command::new("cat").arg("foot.txt").output() { Ok(output) => output, Err(e) => fail!("failed to execute process: {}", e), }; println!("status: {}", output.status); println!("stdout: {}", str::from_utf8_lossy(output.output.as_slice())); println!("stderr: {}", str::from_utf8_lossy(output.error.as_slice())); }use std::io::Command; use std::str; let output = match Command::new("cat").arg("foot.txt").output() { Ok(output) => output, Err(e) => fail!("failed to execute process: {}", e), }; println!("status: {}", output.status); println!("stdout: {}", str::from_utf8_lossy(output.output.as_slice())); println!("stderr: {}", str::from_utf8_lossy(output.error.as_slice()));
fn status(&self) -> IoResult<ProcessExit>
Executes a command as a child process, waiting for it to finish and collecting its exit status.
Example
fn main() { use std::io::Command; let status = match Command::new("ls").status() { Ok(status) => status, Err(e) => fail!("failed to execute process: {}", e), }; println!("process exited with: {}", status); }use std::io::Command; let status = match Command::new("ls").status() { Ok(status) => status, Err(e) => fail!("failed to execute process: {}", e), }; println!("process exited with: {}", status);