Simple Child Process
Spawn a child process with piped stdout/stderr for output capture.
Overview
This is the most basic usage of ProcessManager: spawn a process, let it run, and stop it when done. The Piped stdio configuration causes ProcessManager to spawn reader threads that forward stdout to tracing::debug! and stderr to tracing::error!, preventing pipe buffer deadlocks.
Example
#![allow(unused)]
fn main() {
use process_manager::{ProcessConfig, ProcessManager, StdioConfig};
let manager = ProcessManager::new();
let config = ProcessConfig::builder()
.command("echo".to_string())
.args(vec!["hello world".to_string()])
.stdout(StdioConfig::Piped)
.stderr(StdioConfig::Piped)
.build();
let id = manager.start("echo-app", &config)?;
// Reader threads capture stdout/stderr and forward to tracing
// Check if the process is still running
println!("Running: {}", manager.is_running(id).unwrap_or(false));
println!("State: {}", manager.state(id).unwrap_or(process_manager::ProcessState::Failed));
// Stop when done
manager.stop(id)?;
}
What Happens Internally
ProcessManager::start()resolvesechoviawhich(since it’s not an absolute path)- A
std::process::Commandis built with the piped stdio configuration - The child is spawned and stored in the
DashMapunder the label"echo-app" - Reader threads are spawned for stdout and stderr, forwarding lines to
tracing manager.stop(id)sendsSIGTERM, waits up to 5 seconds, then escalates toSIGKILLif needed