Struct process::Child

Overview

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.

Methods

fn stdin(self) -> Option

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 stdin = child.stdin()?;
fn stdout(self) -> Option

The handle for reading from the child's standard output (stdout), if it has been captured. You might find it helpful to do

let stdout = child.stdout.take()?;

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

fn stderr(self) -> Option

The handle for reading from the child's standard error (stderr), if it has been captured. You might find it helpful to do

let stderr = child.stderr()?;

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

fn id(self) -> Option

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.

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) -> Result

Forces the child to exit.

This is equivalent to sending a SIGKILL on unix platforms.

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 process::Command;

let child = Command::new("sleep");
child.arg("1");

let child = child.spawn();

let recv = wait_for_something();

select {
   _ = child.wait() => {}
   _ = recv => child.kill().await.expect("kill failed"),
}
async fn wait(self) -> Result

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.

use process::{Command, Stdio};

let child = Command::new("cat");
child.stdin(Stdio::piped());

let child = child.spawn()?;

let stdin = child.stdin()?;

// wait for the process to complete
let _ = child.wait().await?;

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.

Protocols

protocol DEBUG_FMT
format!("{:?}", value)

Allows the value to be debug printed.