pub struct GroupChild { /* private fields */ }
Expand description

Representation of a running or exited child process group.

This wraps the Child type in the standard library with methods that work with process groups.

Examples

use std::process::Command;
use command_group::CommandGroup;

let mut child = Command::new("/bin/cat")
                        .arg("file.txt")
                        .group_spawn()
                        .expect("failed to execute child");

let ecode = child.wait()
                 .expect("failed to wait on child");

assert!(ecode.success());

Implementations§

source§

impl GroupChild

source

pub fn inner(&mut self) -> &mut Child

Returns the stdlib Child object.

Note that the inner child may not be in the same state as this output child, due to how methods like wait and kill are implemented. It is not recommended to use this method after using any of the other methods on this struct.

Examples

Reading from stdout:

use std::io::Read;
use std::process::{Command, Stdio};
use command_group::CommandGroup;

let mut child = Command::new("ls").stdout(Stdio::piped()).group_spawn().expect("ls command didn't start");
let mut output = String::new();
if let Some(mut out) = child.inner().stdout.take() {
    out.read_to_string(&mut output).expect("failed to read from child");
}
println!("output: {}", output);
source

pub fn into_inner(self) -> Child

Consumes itself and returns the stdlib Child object.

Note that the inner child may not be in the same state as this output child, due to how methods like wait and kill are implemented. It is not recommended to use this method after using any of the other methods on this struct.

Examples

Writing to input:

use std::io::Write;
use std::process::{Command, Stdio};
use command_group::CommandGroup;

let mut child = Command::new("cat").stdin(Stdio::piped()).group_spawn().expect("cat command didn't start");
if let Some(mut din) = child.into_inner().stdin.take() {
     din.write_all(b"Woohoo!").expect("failed to write");
}
source

pub fn kill(&mut self) -> Result<()>

Forces the child process group to exit.

If the group has already exited, an InvalidInput error is returned.

This is equivalent to sending a SIGKILL on Unix platforms.

See the stdlib documentation for more.

Examples

Basic usage:

use std::process::Command;
use command_group::CommandGroup;

let mut command = Command::new("yes");
if let Ok(mut child) = command.group_spawn() {
    child.kill().expect("command wasn't running");
} else {
    println!("yes command didn't start");
}
source

pub fn id(&self) -> u32

Returns the OS-assigned process group identifier.

See the stdlib documentation for more.

Examples

Basic usage:

use std::process::Command;
use command_group::CommandGroup;

let mut command = Command::new("ls");
if let Ok(child) = command.group_spawn() {
    println!("Child group's ID is {}", child.id());
} else {
    println!("ls command didn't start");
}
source

pub fn wait(&mut self) -> Result<ExitStatus>

Waits for the child group to exit completely, returning the status that the process leader exited with.

See the stdlib documentation for more.

Examples

Basic usage:

use std::process::Command;
use command_group::CommandGroup;

let mut command = Command::new("ls");
if let Ok(mut child) = command.group_spawn() {
    child.wait().expect("command wasn't running");
    println!("Child has finished its execution!");
} else {
    println!("ls command didn't start");
}
source

pub fn try_wait(&mut self) -> Result<Option<ExitStatus>>

Attempts to collect the exit status of the child if it has already exited.

See the stdlib documentation for more.

Examples

Basic usage:

use std::process::Command;
use command_group::CommandGroup;

let mut child = Command::new("ls").group_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),
}
source

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.

See the stdlib documentation for more.

Bugs

On Windows, STDOUT is read before STDERR if both are piped, which may block. This is mostly because reading two outputs at the same time in synchronous code is horrendous. If you want this, please contribute a better version. Alternatively, prefer using the async API.

Examples

Basic usage:

use std::process::{Command, Stdio};
use command_group::CommandGroup;

let child = Command::new("/bin/cat")
    .arg("file.txt")
    .stdout(Stdio::piped())
    .group_spawn()
    .expect("failed to execute child");

let output = child
    .wait_with_output()
    .expect("failed to wait on child");

assert!(output.status.success());

Trait Implementations§

source§

impl Debug for GroupChild

source§

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

Formats the value using the given formatter. Read more
source§

impl UnixChildExt for GroupChild

source§

fn signal(&self, sig: Signal) -> Result<()>

Sends a signal to the child process. If the process has already exited, an InvalidInput error is returned. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.