Crate process_control
source · [−]Expand description
This crate allows running a process with resource limits, such as a running time, and the option to terminate it automatically afterward. The latter is surprisingly difficult to achieve on Unix, since process identifiers can be arbitrarily reassigned when no longer used. Thus, it would be extremely easy to inadvertently terminate an unexpected process. This crate protects against that possibility.
Methods for setting limits are available on ChildExt
, which is
implemented for Child
. They each return a builder of options to
configure how the limit should be applied.
Features
These features are optional and can be enabled or disabled in a “Cargo.toml” file.
Optional Features
- crossbeam-channel - Changes the implementation to use crate crossbeam-channel for better performance.
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.
Comparable Crates
- wait-timeout -
Made for a related purpose but does not provide the same functionality.
Processes cannot be terminated automatically, and there is no counterpart
of
ChildExt::controlled_with_output
to read output while setting a timeout. This crate aims to fill in those gaps and simplify the implementation, now thatReceiver::recv_timeout
exists.
Examples
use std::io;
use std::process::Command;
use std::process::Stdio;
use std::time::Duration;
use process_control::ChildExt;
use process_control::Control;
let process = Command::new("echo")
.arg("hello")
.stdout(Stdio::piped())
.spawn()?;
let output = process
.controlled_with_output()
.time_limit(Duration::from_secs(1))
.terminate_for_timeout()
.wait()?
.ok_or_else(|| {
io::Error::new(io::ErrorKind::TimedOut, "Process timed out")
})?;
assert_eq!(b"hello", &output.stdout[..5]);
Structs
Equivalent to process::ExitStatus
but allows for greater accuracy.
Equivalent to process::Output
but holds an instance of ExitStatus
from this crate.
A wrapper that stores enough information to terminate a process.