[][src]Crate process_control

This crate allows terminating a process without a mutable reference. Thus, it becomes possible to abort early from waiting for output or an exit code – primarily through ProcessTerminator::terminate. That method is intentionally designed to not require a reference of any kind to the Child instance, to allow for maximal flexibility.

Typically, it is not possible to terminate a process during a call to Child::wait or Child::wait_with_output in another thread, since Child::kill takes a mutable reference. However, since this crate creates its own termination method, there is no issue, and useful methods such as Terminator::wait_for_output_with_timeout can exist.

Crate wait-timeout has a similar purpose, but it does not provide the same flexibility. It does not allow reading the entire output of a process within the time limit or terminating a process based on other signals. This crate aims to fill in those gaps and simplify the implementation, now that Receiver::recv_timeout exists.

Implementation

All traits are sealed, meaning that they can only be implemented by this crate. Otherwise, backward compatibility would be more difficult to maintain for new features.

Examples

use std::io::Error as IoError;
use std::io::ErrorKind as IoErrorKind;
use std::process::Command;
use std::process::Stdio;
use std::time::Duration;

use process_control::Terminator;

let process = Command::new("echo")
    .arg("hello")
    .stdout(Stdio::piped())
    .spawn()?;

let output = process
    .wait_for_output_with_timeout(Duration::from_secs(1))?
    .ok_or_else(|| {
        IoError::new(IoErrorKind::TimedOut, "Process timed out")
    })?;
assert_eq!(b"hello", &output.stdout[..5]);

Structs

ProcessTerminator

A wrapper that stores enough information to terminate a process.

Traits

Terminator

Extensions to Child for easily killing processes.