job-limits 0.1.0

Memory limits for child processes, and a guard that cleans up the whole process tree (same API on unix and Windows)
Documentation
//! Spawns the given command with a 256 MiB memory limit and waits for it to end.
//!
//! Run: `cargo run --example run -- <program> [args...]`

use std::process::{Command, exit};

use job_limits::Limits;

fn main() -> std::io::Result<()> {
    let mut args = std::env::args().skip(1);
    let Some(program) = args.next() else {
        eprintln!("usage: run <program> [args...]");
        exit(2);
    };
    let mut command = Command::new(program);
    command.args(args);

    let (mut child, guard) = Limits::new()
        .process_memory(256 << 20)
        .spawn(&mut command)?;
    let status = child.wait()?;
    // Ends any grandchildren the child left behind
    drop(guard);

    println!("exit code: {:?}", status.code());
    Ok(())
}