Struct base::sys::unix::signal::KillOnDrop

source ·
pub struct KillOnDrop {
    process: Child,
    timeout: Duration,
}
Expand description

Wraps a Child process, and calls kill_tree for its process group to clean it up when dropped.

Fields§

§process: Child§timeout: Duration

Implementations§

Get the timeout. See timeout_mut() for more details.

Change the timeout for how long child processes are waited for before the process group is forcibly killed.

Methods from Deref<Target = Child>§

Forces the child process to exit. If the child has already exited, an InvalidInput error is returned.

The mapping to ErrorKinds 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 wasn't running");
} else {
    println!("yes command didn't start");
}

Returns the OS-assigned process identifier associated with this child.

Examples

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");
}

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");
}

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}"),
}

Trait Implementations§

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Executes the destructor for this type. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.