pub struct Command { /* fields omitted */ }
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 = if cfg!(target_os = "windows") {
Command::new("cmd")
.args(&["/C", "echo hello"])
.output()
.expect("failed to execute process")
} else {
Command::new("sh")
.arg("-c")
.arg("echo hello")
.output()
.expect("failed to execute process")
};
let hello = output.stdout; impl Command
[src]
fn new<S: AsRef<OsStr>>(program: S) -> CommandConstructs a new Command for launching the program at path program, with the following default configuration:
spawn or status, but create pipes for output
Builder methods are provided to change these defaults and otherwise configure the process.
If program is not an absolute path, the PATH will be searched in an OS-defined way.
The search path to be used may be controlled by setting the PATH environment variable on the Command, but this has some implementation limitations on Windows (see https://github.com/rust-lang/rust/issues/37519).
Basic usage:
use std::process::Command;
Command::new("sh")
.spawn()
.expect("sh command failed to start"); fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut CommandAdd an argument to pass to the program.
Basic usage:
use std::process::Command;
Command::new("ls")
.arg("-l")
.arg("-a")
.spawn()
.expect("ls command failed to start"); fn args<I, S>(&mut self, args: I) -> &mut Command where I: IntoIterator<Item=S>, S: AsRef<OsStr>Add multiple arguments to pass to the program.
Basic usage:
use std::process::Command;
Command::new("ls")
.args(&["-l", "-a"])
.spawn()
.expect("ls command failed to start"); 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.
Basic usage:
use std::process::Command;
Command::new("ls")
.env("PATH", "/bin")
.spawn()
.expect("ls command failed to start"); fn envs<I, K, V>(&mut self, vars: I) -> &mut Command where I: IntoIterator<Item=(K, V)>, K: AsRef<OsStr>, V: AsRef<OsStr>Add or update multiple environment variable mappings.
Basic usage: ```no_run use std::process::{Command, Stdio}; use std::env; use std::collections::HashMap;
let filtered_env : HashMap
Command::new("printenv") .stdin(Stdio::null()) .stdout(Stdio::inherit()) .env_clear() .envs(&filtered_env) .spawn() .expect("printenv failed to start"); ```
fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut CommandRemoves an environment variable mapping.
Basic usage:
use std::process::Command;
Command::new("ls")
.env_remove("PATH")
.spawn()
.expect("ls command failed to start"); fn env_clear(&mut self) -> &mut CommandClears the entire environment map for the child process.
Basic usage:
use std::process::Command;
Command::new("ls")
.env_clear()
.spawn()
.expect("ls command failed to start"); fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut CommandSets the working directory for the child process.
Basic usage:
use std::process::Command;
Command::new("ls")
.current_dir("/bin")
.spawn()
.expect("ls command failed to start"); fn stdin(&mut self, cfg: Stdio) -> &mut CommandConfiguration for the child process's stdin handle (file descriptor 0).
Basic usage:
use std::process::{Command, Stdio};
Command::new("ls")
.stdin(Stdio::null())
.spawn()
.expect("ls command failed to start"); fn stdout(&mut self, cfg: Stdio) -> &mut CommandConfiguration for the child process's stdout handle (file descriptor 1).
Basic usage:
use std::process::{Command, Stdio};
Command::new("ls")
.stdout(Stdio::null())
.spawn()
.expect("ls command failed to start"); fn stderr(&mut self, cfg: Stdio) -> &mut CommandConfiguration for the child process's stderr handle (file descriptor 2).
Basic usage:
use std::process::{Command, Stdio};
Command::new("ls")
.stderr(Stdio::null())
.spawn()
.expect("ls command failed to start"); 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.
Basic usage:
use std::process::Command;
Command::new("ls")
.spawn()
.expect("ls command failed to start"); 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).
use std::process::Command;
let output = Command::new("/bin/cat")
.arg("file.txt")
.output()
.expect("failed to execute process");
println!("status: {}", output.status);
println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
assert!(output.status.success()); 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.
use std::process::Command;
let status = Command::new("/bin/cat")
.arg("file.txt")
.status()
.expect("failed to execute process");
println!("process exited with: {}", status);
assert!(status.success()); impl Debug for Command
[src]
fn fmt(&self, f: &mut Formatter) -> ResultFormat the program and arguments of a Command for display. Any non-utf8 data is lossily converted using the utf8 replacement character.
impl CommandExt for Command
[src]
fn uid(&mut self, id: u32) -> &mut CommandSets the child process's user id. This translates to a setuid call in the child process. Failure in the setuid call will cause the spawn to fail. Read more
fn gid(&mut self, id: u32) -> &mut CommandSimilar to uid, but sets the group id of the child process. This has the same semantics as the uid field. Read more
fn before_exec<F>(&mut self, f: F) -> &mut Command where F: FnMut() -> Result<()> + Send + Sync + 'staticSchedules a closure to be run just before the exec function is invoked. Read more
fn exec(&mut self) -> ErrorPerforms all the required setup by this Command, followed by calling the execvp syscall. Read more
© 2010 The Rust Project Developers
Licensed under the Apache License, Version 2.0 or the MIT license, at your option.
https://doc.rust-lang.org/std/process/struct.Command.html