proc_guard

Crates.ioproc_guard
lib.rsproc_guard
version0.2.0
sourcesrc
created_at2024-07-19 22:01:13.202226
updated_at2024-07-30 17:45:02.368893
descriptionA process guard that ensures the proper destruction of a child process in various scenarios
homepage
repositoryhttps://github.com/fplu/proc_guard
max_upload_size
id1309097
size38,860
(fplu)

documentation

README

proc_guard

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.

Features

  • Different termination strategies such as waiting, sending Ctrl+C, and killing the process.
  • Ensures the proper cleanup of child processes when the guard goes out of scope.
  • Supports both blocking and timeout-based termination methods.

Installation

Add the following to your Cargo.toml:

[dependencies]
proc_guard = "0.2.0"

Usage

Below are some examples to illustrate how to use the ProcGuard crate.

Example 1: Basic Usage

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(())
}

Example 2: Using Ctrl+C and Wait

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(())
}

Example 3: Spawn a New Guarded Process

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(())
}

Example 4: Releasing the Guard

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(())
}

Termination Strategies

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.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Commit count: 0

cargo fmt