| Crates.io | proc_guard |
| lib.rs | proc_guard |
| version | 0.2.0 |
| created_at | 2024-07-19 22:01:13.202226+00 |
| updated_at | 2024-07-30 17:45:02.368893+00 |
| description | A process guard that ensures the proper destruction of a child process in various scenarios |
| homepage | |
| repository | https://github.com/fplu/proc_guard |
| max_upload_size | |
| id | 1309097 |
| size | 38,860 |
proc_guard is a Rust crate designed to manage and ensure the proper termination of child processes using various termination strategies. This crate provides a guard for a child process that ensures the process is terminated according to a specified strategy when the guard is dropped.
Add the following to your Cargo.toml:
[dependencies]
proc_guard = "0.2.0"
Below are some examples to illustrate how to use the ProcGuard crate.
use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let child = if cfg!(target_os = "windows") {
Command::new("timeout").args(["/t", "2"]).spawn()?
} else {
Command::new("sleep").arg("2").spawn()?
};
let guard = ProcGuard::new(child, ProcessTermination::Wait);
// The child process will be waited upon until it exits.
Ok(())
}
use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let child = if cfg!(target_os = "windows") {
Command::new("timeout").args(["/t", "2"]).spawn()?
} else {
Command::new("sleep").arg("2").spawn()?
};
let guard = ProcGuard::new(child, ProcessTermination::CtrlCWait);
// The child process will receive a Ctrl+C signal and will be waited upon until it exits.
Ok(())
}
use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let guard = if cfg!(target_os = "windows") {
ProcGuard::spawn(Command::new("timeout").args(["/t", "2"]), ProcessTermination::CtrlCWait)?
} else {
ProcGuard::spawn(Command::new("sleep").arg("2"), ProcessTermination::CtrlCWait)?
};
// The child process will be managed and terminated as specified.
Ok(())
}
use std::process::Command;
use proc_guard::{ProcGuard, ProcessTermination};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let child = if cfg!(target_os = "windows") {
Command::new("timeout").args(["/t", "2"]).spawn()?
} else {
Command::new("sleep").arg("2").spawn()?
};
let guard = ProcGuard::new(child, ProcessTermination::Wait);
let child = guard.release();
// The child process is now unmanaged and must be manually handled.
Ok(())
}
The ProcessTermination enum provides various strategies for terminating a process:
Wait: Wait indefinitely for the process to exit.WaitTimeout(Duration): Wait for a specified duration for the process to exit.WaitTimeoutKill(Duration): Wait for a specified duration, then kill the process if it hasn't exited.CtrlC: Send a Ctrl+C signal to the process and does not wait.CtrlCWait: Send a Ctrl+C signal and wait indefinitely for the process to exit.CtrlCWaitTimeout(Duration): Send a Ctrl+C signal and wait for a specified duration for the process to exit.CtrlCWaitTimeoutKill(Duration): Send a Ctrl+C signal, wait for a specified duration, and then kill the process if it hasn't exited.Kill: Kill the process immediately and does not wait.KillWait: Kill the process immediately and wait indefinitely for the process to exit.This project is licensed under the MIT License. See the LICENSE file for details.