Struct Child

struct Child { ... }

Representation of a child process spawned onto an event loop.

Caveats

Similar to the behavior to the standard library, and unlike the futures paradigm of dropping-implies-cancellation, a spawned process will, by default, continue to execute even after the Child handle has been dropped.

The Command::kill_on_drop method can be used to modify this behavior and kill the child process if the Child wrapper is dropped before it has exited.

Fields

stdin: Option<ChildStdin>

The handle for writing to the child's standard input (stdin), if it has been captured. To avoid partially moving the child and thus blocking yourself from calling functions on child while using stdin, you might find it helpful to do:

# let mut child = tokio::process::Command::new("echo").spawn().unwrap();
let stdin = child.stdin.take().unwrap();
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

# let mut child = tokio::process::Command::new("echo").spawn().unwrap();
let stdout = child.stdout.take().unwrap();

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

# let mut child = tokio::process::Command::new("echo").spawn().unwrap();
let stderr = child.stderr.take().unwrap();

to avoid partially moving the child and thus blocking yourself from calling functions on child while using stderr.

Implementations

impl Child

fn id(self: &Self) -> Option<u32>

Returns the OS-assigned process identifier associated with this child while it is still running.

Once the child has been polled to completion this will return None. This is done to avoid confusion on platforms like Unix where the OS identifier could be reused once the process has completed.

fn start_kill(self: &mut Self) -> io::Result<()>

Attempts to force the child to exit, but does not wait for the request to take effect.

On Unix platforms, this is the equivalent to sending a SIGKILL. Note that on Unix platforms it is possible for a zombie process to remain after a kill is sent; to avoid this, the caller should ensure that either child.wait().await or child.try_wait() is invoked successfully.

async fn kill(self: &mut Self) -> io::Result<()>

Forces the child to exit.

This is equivalent to sending a SIGKILL on unix platforms followed by wait.

Note: std version of Child::kill does not wait. For an equivalent of Child::kill in the standard library, use start_kill.

Examples

If the child has to be killed remotely, it is possible to do it using a combination of the select! macro and a oneshot channel. In the following example, the child will run until completion unless a message is sent on the oneshot channel. If that happens, the child is killed immediately using the .kill() method.

use tokio::process::Command;
use tokio::sync::oneshot::channel;

#[tokio::main]
async fn main() {
    let (send, recv) = channel::<()>();
    let mut child = Command::new("sleep").arg("1").spawn().unwrap();
    tokio::spawn(async move { send.send(()) });
    tokio::select! {
        _ = child.wait() => {}
        _ = recv => child.kill().await.expect("kill failed"),
    }
}

You can also interact with the child's standard I/O. For example, you can read its stdout while waiting for it to exit.

# use std::process::Stdio;
#
# use tokio::io::AsyncReadExt;
# use tokio::process::Command;
# use tokio::sync::oneshot::channel;

#[tokio::main]
async fn main() {
    let (_tx, rx) = channel::<()>();

    let mut child = Command::new("echo")
        .arg("Hello World!")
        .stdout(Stdio::piped())
        .spawn()
        .unwrap();

    let mut stdout = child.stdout.take().expect("stdout is not captured");

    let read_stdout = tokio::spawn(async move {
        let mut buff = Vec::new();
        let _ = stdout.read_to_end(&mut buff).await;

        buff
    });

    tokio::select! {
        _ = child.wait() => {}
        _ = rx => { child.kill().await.expect("kill failed") },
    }

    let buff = read_stdout.await.unwrap();

    assert_eq!(buff, b"Hello World!\n");
}
async fn wait(self: &mut Self) -> io::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.

If the caller wishes to explicitly control when the child's stdin handle is closed, they may .take() it before calling .wait():

Cancel safety

This function is cancel safe.

# #[cfg(not(unix))]fn main(){}
# #[cfg(unix)]
use tokio::io::AsyncWriteExt;
# #[cfg(unix)]
use tokio::process::Command;
# #[cfg(unix)]
use std::process::Stdio;

# #[cfg(unix)]
#[tokio::main]
async fn main() {
#   if cfg!(miri) { return; } // No `pidfd_spawnp` in miri.
    let mut child = Command::new("cat")
        .stdin(Stdio::piped())
        .spawn()
        .unwrap();

    let mut stdin = child.stdin.take().unwrap();
    tokio::spawn(async move {
        // do something with stdin here...
        stdin.write_all(b"hello world\n").await.unwrap();

        // then drop when finished
        drop(stdin);
    });

    // wait for the process to complete
    let _ = child.wait().await;
}
fn try_wait(self: &mut Self) -> io::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, nor will it wake the current task if the child exits.

async fn wait_with_output(self: Self) -> io::Result<Output>

Returns a future that will resolve to an Output, containing the exit status, stdout, and stderr of the child process.

The returned future will 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 Output it is necessary to create new pipes between parent and child. Use stdout(Stdio::piped()) or stderr(Stdio::piped()), respectively, when creating a Command.

impl Debug for Child

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Freeze for Child

impl RefUnwindSafe for Child

impl Send for Child

impl Sync for Child

impl Unpin for Child

impl UnwindSafe for Child

impl<T> Any for Child

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Child

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Child

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> From for Child

fn from(t: T) -> T

Returns the argument unchanged.

impl<T, U> Into for Child

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Child

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Child

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>