tokio::process module provides asynchronous versions of process management functions for spawning and interacting with child processes.
Overview
This module provides aCommand type that mirrors std::process::Command but with asynchronous methods. The asynchronous process support works through signal handling on Unix and system APIs on Windows.
Quick Start
Spawn a simple command and wait for it to complete:Command
TheCommand type is used to configure and spawn child processes.
Creating Commands
Constructs a new Command for launching a program.Parameters:
program: impl AsRef<OsStr>- Path to the program
CommandConfiguring Commands
Adds an argument to pass to the program.Parameters:
arg: impl AsRef<OsStr>- Argument to add
&mut CommandAdds multiple arguments to pass to the program.Parameters:
args: impl IntoIterator<Item = impl AsRef<OsStr>>- Arguments to add
&mut CommandInserts or updates an environment variable.Parameters:
key: impl AsRef<OsStr>- Variable nameval: impl AsRef<OsStr>- Variable value
&mut CommandAdds or updates multiple environment variables.Parameters:
vars: impl IntoIterator<Item = (K, V)>- Key-value pairs
&mut CommandRemoves an environment variable.Parameters:
key: impl AsRef<OsStr>- Variable to remove
&mut CommandClears all environment variables.Returns:
&mut CommandSets the working directory for the child process.Parameters:
dir: impl AsRef<Path>- Working directory
&mut CommandStandard I/O Configuration
Configures the child process’s standard input.Parameters:
cfg: impl Into<Stdio>- Stdio configuration
&mut CommandOptions:Stdio::inherit()- Inherit from parent (default)Stdio::piped()- Create a pipeStdio::null()- Redirect to null
Configures the child process’s standard output.Parameters:
cfg: impl Into<Stdio>- Stdio configuration
&mut CommandConfigures the child process’s standard error.Parameters:
cfg: impl Into<Stdio>- Stdio configuration
&mut CommandControls whether the child is killed when the Child handle is dropped.Parameters:
kill_on_drop: bool- Whether to kill on drop
&mut CommandSpawning Processes
Spawns the configured command as a child process.Returns:
io::Result<Child>Returns immediately with a handle to the child process.Spawns the command and waits for it to finish.Returns:
impl Future<Output = io::Result<ExitStatus>>Closes any piped stdin/stdout/stderr before waiting.Spawns the command and collects all of its output.Returns:
impl Future<Output = io::Result<Output>>Automatically configures stdout and stderr as piped.Child Process
TheChild struct represents a running child process.
Fields
Methods
Returns the OS process identifier.Returns:
Option<u32>Returns None after the process has been polled to completion.Waits for the child to exit completely.Returns:
io::Result<ExitStatus>Closes stdin before waiting to avoid deadlocks. This method is cancel safe.Attempts to collect the exit status without blocking.Returns:
io::Result<Option<ExitStatus>>Returns Ok(None) if the child hasn’t exited yet.Waits for the child and collects all output.Returns:
io::Result<Output>Forces the child to exit and waits for it.Returns:
io::Result<()>Sends SIGKILL on Unix platforms.Forces the child to exit without waiting.Returns:
io::Result<()>This is the async equivalent of std::process::Child::kill.Examples
Capture Command Output
Read Output Line by Line
Write to Child’s Stdin
Pipe Between Commands
Kill Child on Signal
Unix-Specific Methods
These methods are only available on Unix platforms.
Sets the user ID for the child process.Parameters:
id: u32- User ID
&mut CommandCalls setuid in the child process.Sets the group ID for the child process.Parameters:
id: u32- Group ID
&mut CommandSets the process group ID (PGID).Parameters:
pgroup: i32- Process group ID (0 uses process ID)
&mut CommandImportant Notes
Dropping vs Cancellation
Zombie Processes on Unix
On Unix, processes must be “reaped” by their parent after exiting. The Tokio runtime will attempt to reap spawned processes on a best-effort basis, but this is not guaranteed.See Also
- std::process::Command - Standard library equivalent
- AsyncRead - For reading from child’s stdout/stderr
- AsyncWrite - For writing to child’s stdin