pub struct Child { pub stdin: Option<ChildStdin>, pub stdout: Option<ChildStdout>, pub stderr: Option<ChildStderr>, // some fields omitted }
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.
use std::process::Command; let mut child = Command::new("/bin/cat") .arg("file.txt") .spawn() .expect("failed to execute child"); let ecode = child.wait() .expect("failed to wait on child"); assert!(ecode.success());
Take note that 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.
stdin: Option<ChildStdin>
The handle for writing to the child's stdin, if it has been captured
stdout: Option<ChildStdout>
The handle for reading from the child's stdout, if it has been captured
stderr: Option<ChildStderr>
The handle for reading from the child's stderr, if it has been captured
impl Child
[src]
fn kill(&mut self) -> Result<()>
Forces the child to exit. This is equivalent to sending a SIGKILL on unix platforms.
Basic usage:
use std::process::Command; let mut command = Command::new("yes"); if let Ok(mut child) = command.spawn() { child.kill().expect("command wasn't running"); } else { println!("yes command didn't start"); }
fn id(&self) -> u32
Returns the OS-assigned process identifier associated with this child.
Basic usage:
use std::process::Command; let mut command = Command::new("ls"); if let Ok(child) = command.spawn() { println!("Child's id is {}", child.id()); } else { println!("ls command didn't start"); }
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.
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"); }
fn try_wait(&mut self) -> Result<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 advisorily 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(status)
is returned. If the exit status is not available at this time then an error is returned with the error kind WouldBlock
. If an error occurs, then that error is returned.
Note that unlike wait
, this function will not attempt to drop stdin.
Basic usage:
#![feature(process_try_wait)] use std::io; use std::process::Command; let mut child = Command::new("ls").spawn().unwrap(); match child.try_wait() { Ok(status) => println!("exited with: {}", status), Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => { println!("status not ready yet, let's really wait"); let res = child.wait(); println!("result: {:?}", res); } Err(e) => println!("error attempting to wait: {}", e), }
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.
use std::process::{Command, Stdio}; let child = Command::new("/bin/cat") .arg("file.txt") .stdout(Stdio::piped()) .spawn() .expect("failed to execute child"); let output = child .wait_with_output() .expect("failed to wait on child"); assert!(output.status.success());
impl Debug for Child
fn fmt(&self, f: &mut Formatter) -> Result
Formats the value using the given formatter.
© 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.Child.html