Struct Command
pub struct Command { /* private fields */ }Expand description
A builder for spawning processes.
§Examples
use async_process::Command;
let output = if cfg!(target_os = "windows") {
Command::new("cmd").args(&["/C", "echo hello"]).output().await?
} else {
Command::new("sh").arg("-c").arg("echo hello").output().await?
};Implementations§
§impl Command
impl Command
pub fn arg<S>(&mut self, arg: S) -> &mut Command
pub fn arg<S>(&mut self, arg: S) -> &mut Command
Adds a single argument to pass to the program.
§Examples
use async_process::Command;
let mut cmd = Command::new("echo");
cmd.arg("hello");
cmd.arg("world");pub fn args<I, S>(&mut self, args: I) -> &mut Command
pub fn args<I, S>(&mut self, args: I) -> &mut Command
Adds multiple arguments to pass to the program.
§Examples
use async_process::Command;
let mut cmd = Command::new("echo");
cmd.args(&["hello", "world"]);pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
Configures an environment variable for the new process.
Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.
§Examples
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.env("PATH", "/bin");pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
Configures multiple environment variables for the new process.
Note that environment variable names are case-insensitive (but case-preserving) on Windows, and case-sensitive on all other platforms.
§Examples
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.envs(vec![("PATH", "/bin"), ("TERM", "xterm-256color")]);pub fn get_args(&self) -> CommandArgs<'_>
pub fn get_args(&self) -> CommandArgs<'_>
Gets an iterator of the arguments that will be passed to the program.
§Examples
use std::ffi::OsStr;
use async_process::Command;
let mut cmd = Command::new("echo");
cmd.arg("first").arg("second");
let args: Vec<&OsStr> = cmd.get_args().collect();
assert_eq!(args, &["first", "second"]);pub fn get_envs(&self) -> CommandEnvs<'_>
pub fn get_envs(&self) -> CommandEnvs<'_>
Gets an iterator of the environment variables explicitly set for the child process.
§Examples
use std::ffi::OsStr;
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.env("TERM", "dumb").env_remove("TZ");
let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
assert_eq!(envs, &[
(OsStr::new("TERM"), Some(OsStr::new("dumb"))),
(OsStr::new("TZ"), None)
]);pub fn get_current_dir(&self) -> Option<&Path>
pub fn get_current_dir(&self) -> Option<&Path>
Gets the working directory for the child process.
This returns None if the working directory will not be changed.
§Examples
use std::path::Path;
use async_process::Command;
let mut cmd = Command::new("ls");
assert_eq!(cmd.get_current_dir(), None);
cmd.current_dir("/bin");
assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));pub fn get_program(&self) -> &OsStr
pub fn get_program(&self) -> &OsStr
Gets the path to the program that was given to Command::new.
§Examples
use async_process::Command;
let cmd = Command::new("echo");
assert_eq!(cmd.get_program(), "echo");pub fn env_remove<K>(&mut self, key: K) -> &mut Command
pub fn env_remove<K>(&mut self, key: K) -> &mut Command
Removes an environment variable mapping.
§Examples
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.env_remove("PATH");pub fn env_clear(&mut self) -> &mut Command
pub fn env_clear(&mut self) -> &mut Command
Removes all environment variable mappings.
§Examples
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.env_clear();pub fn current_dir<P>(&mut self, dir: P) -> &mut Command
pub fn current_dir<P>(&mut self, dir: P) -> &mut Command
Configures the working directory for the new process.
§Examples
use async_process::Command;
let mut cmd = Command::new("ls");
cmd.current_dir("/");pub fn stdin<T>(&mut self, cfg: T) -> &mut Command
pub fn stdin<T>(&mut self, cfg: T) -> &mut Command
Configures the standard input (stdin) for the new process.
§Examples
use async_process::{Command, Stdio};
let mut cmd = Command::new("cat");
cmd.stdin(Stdio::null());pub fn stdout<T>(&mut self, cfg: T) -> &mut Command
pub fn stdout<T>(&mut self, cfg: T) -> &mut Command
Configures the standard output (stdout) for the new process.
§Examples
use async_process::{Command, Stdio};
let mut cmd = Command::new("ls");
cmd.stdout(Stdio::piped());pub fn stderr<T>(&mut self, cfg: T) -> &mut Command
pub fn stderr<T>(&mut self, cfg: T) -> &mut Command
Configures the standard error (stderr) for the new process.
§Examples
use async_process::{Command, Stdio};
let mut cmd = Command::new("ls");
cmd.stderr(Stdio::piped());pub fn reap_on_drop(&mut self, reap_on_drop: bool) -> &mut Command
pub fn reap_on_drop(&mut self, reap_on_drop: bool) -> &mut Command
Configures whether to reap the zombie process when Child is dropped.
When the process finishes, it becomes a “zombie” and some resources associated with it
remain until Child::try_status(), Child::status(), or Child::output() collects
its exit code.
If its exit code is never collected, the resources may leak forever. This crate has a background thread named “async-process” that collects such “zombie” processes and then “reaps” them, thus preventing the resource leaks.
The default value of this option is true.
§Examples
use async_process::{Command, Stdio};
let mut cmd = Command::new("cat");
cmd.reap_on_drop(false);pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Command
pub fn kill_on_drop(&mut self, kill_on_drop: bool) -> &mut Command
pub fn spawn(&mut self) -> Result<Child, Error>
pub fn spawn(&mut self) -> Result<Child, Error>
Executes the command and returns the Child handle to it.
If not configured, stdin, stdout and stderr will be set to Stdio::inherit().
§Examples
use async_process::Command;
let child = Command::new("ls").spawn()?;pub fn status(&mut self) -> impl Future<Output = Result<ExitStatus, Error>>
pub fn status(&mut self) -> impl Future<Output = Result<ExitStatus, Error>>
Executes the command, waits for it to exit, and returns the exit status.
If not configured, stdin, stdout and stderr will be set to Stdio::inherit().
§Examples
use async_process::Command;
let status = Command::new("cp")
.arg("a.txt")
.arg("b.txt")
.status()
.await?;pub fn output(&mut self) -> impl Future<Output = Result<Output, Error>>
pub fn output(&mut self) -> impl Future<Output = Result<Output, Error>>
Executes the command and collects its output.
If not configured, stdin will be set to Stdio::null(), and stdout and stderr will be
set to Stdio::piped().
§Examples
use async_process::Command;
let output = Command::new("cat")
.arg("a.txt")
.output()
.await?;Trait Implementations§
§impl CommandExt for Command
impl CommandExt for Command
§fn uid(&mut self, id: u32) -> &mut Command
fn uid(&mut self, id: u32) -> &mut Command
setuid call in the child process. Failure in the setuid
call will cause the spawn to fail.§fn gid(&mut self, id: u32) -> &mut Command
fn gid(&mut self, id: u32) -> &mut Command
uid, but sets the group ID of the child process. This has
the same semantics as the uid field.Auto Trait Implementations§
impl Freeze for Command
impl !RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl !UnwindSafe for Command
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more