pub struct Child {
pub(crate) handle: Process,
pub stdin: Option<ChildStdin>,
pub stdout: Option<ChildStdout>,
pub stderr: Option<ChildStderr>,
}
Expand description
Representation of a running or exited child process.
This structure is used to represent and manage child processes. A child
process is created via the Command
struct, which configures the
spawning process and can itself be constructed using a builder-style
interface.
There is no implementation of Drop
for child processes,
so if you do not ensure the Child
has exited then it will continue to
run, even after the Child
handle to the child process has gone out of
scope.
Calling wait
(or other functions that wrap around it) will make
the parent process wait until the child has actually exited before
continuing.
Warning
On some systems, calling wait
or similar is necessary for the OS to
release resources. A process that terminated but has not been waited on is
still around as a “zombie”. Leaving too many zombies around may exhaust
global resources (for example process IDs).
The standard library does not automatically wait on child processes (not
even if the Child
is dropped), it is up to the application developer to do
so. As a consequence, dropping Child
handles without waiting on them first
is not recommended in long-running applications.
Examples
Fields§
§handle: Process
§stdin: Option<ChildStdin>
The handle for writing to the child’s standard input (stdin), if it has been captured. You might find it helpful to do
to avoid partially moving the child
and thus blocking yourself from calling
functions on child
while using stdin
.
stdout: Option<ChildStdout>
The handle for reading from the child’s standard output (stdout), if it has been captured. You might find it helpful to do
to avoid partially moving the child
and thus blocking yourself from calling
functions on child
while using stdout
.
stderr: Option<ChildStderr>
The handle for reading from the child’s standard error (stderr), if it has been captured. You might find it helpful to do
to avoid partially moving the child
and thus blocking yourself from calling
functions on child
while using stderr
.
Implementations§
source§impl Child
impl Child
sourcepub fn kill(&mut self) -> Result<()>
pub fn kill(&mut self) -> Result<()>
Forces the child process to exit. If the child has already exited, Ok(())
is returned.
The mapping to ErrorKind
s is not part of the compatibility contract of the function.
This is equivalent to sending a SIGKILL on Unix platforms.
Examples
Basic usage:
use std::process::Command;
let mut command = Command::new("yes");
if let Ok(mut child) = command.spawn() {
child.kill().expect("command couldn't be killed");
} else {
println!("yes command didn't start");
}
Runsourcepub fn wait(&mut self) -> Result<ExitStatus>
pub fn wait(&mut self) -> Result<ExitStatus>
Waits for the child to exit completely, returning the status that it exited with. This function will continue to have the same return value after it has been called at least once.
The stdin handle to the child process, if any, will be closed before waiting. This helps avoid deadlock: it ensures that the child does not block waiting for input from the parent, while the parent waits for the child to exit.
Examples
Basic usage:
use std::process::Command;
let mut command = Command::new("ls");
if let Ok(mut child) = command.spawn() {
child.wait().expect("command wasn't running");
println!("Child has finished its execution!");
} else {
println!("ls command didn't start");
}
Run1.18.0 · sourcepub fn try_wait(&mut self) -> Result<Option<ExitStatus>>
pub fn try_wait(&mut self) -> Result<Option<ExitStatus>>
Attempts to collect the exit status of the child if it has already exited.
This function will not block the calling thread and will only check to see if the child process has exited or not. If the child has exited then on Unix the process ID is reaped. This function is guaranteed to repeatedly return a successful exit status so long as the child has already exited.
If the child has exited, then Ok(Some(status))
is returned. If the
exit status is not available at this time then Ok(None)
is returned.
If an error occurs, then that error is returned.
Note that unlike wait
, this function will not attempt to drop stdin.
Examples
Basic usage:
use std::process::Command;
let mut child = Command::new("ls").spawn().unwrap();
match child.try_wait() {
Ok(Some(status)) => println!("exited with: {status}"),
Ok(None) => {
println!("status not ready yet, let's really wait");
let res = child.wait();
println!("result: {res:?}");
}
Err(e) => println!("error attempting to wait: {e}"),
}
Runsourcepub fn wait_with_output(self) -> Result<Output>
pub fn wait_with_output(self) -> Result<Output>
Simultaneously waits for the child to exit and collect all remaining
output on the stdout/stderr handles, returning an Output
instance.
The stdin handle to the child process, if any, will be closed before waiting. This helps avoid deadlock: it ensures that the child does not block waiting for input from the parent, while the parent waits for the child to exit.
By default, stdin, stdout and stderr are inherited from the parent.
In order to capture the output into this Result<Output>
it is
necessary to create new pipes between parent and child. Use
stdout(Stdio::piped())
or stderr(Stdio::piped())
, respectively.
Examples
Trait Implementations§
1.63.0 · source§impl AsHandle for Child
Available on Windows only.
impl AsHandle for Child
source§fn as_handle(&self) -> BorrowedHandle<'_>
fn as_handle(&self) -> BorrowedHandle<'_>
1.2.0 · source§impl AsRawHandle for Child
Available on Windows only.
impl AsRawHandle for Child
source§fn as_raw_handle(&self) -> RawHandle
fn as_raw_handle(&self) -> RawHandle
source§impl ChildExt for Child
Available on Windows only.
impl ChildExt for Child
source§fn main_thread_handle(&self) -> BorrowedHandle<'_>
fn main_thread_handle(&self) -> BorrowedHandle<'_>
windows_process_extensions_main_thread_handle
#96723)source§impl ChildExt for Child
impl ChildExt for Child
1.63.0 · source§impl From<Child> for OwnedHandle
Available on Windows only.
impl From<Child> for OwnedHandle
source§fn from(child: Child) -> OwnedHandle
fn from(child: Child) -> OwnedHandle
1.4.0 · source§impl IntoRawHandle for Child
Available on Windows only.
impl IntoRawHandle for Child
source§fn into_raw_handle(self) -> RawHandle
fn into_raw_handle(self) -> RawHandle
impl Sealed for Child
Allows extension traits within std
.