process_state

Crates.ioprocess_state
lib.rsprocess_state
version1.0.0
created_at2025-11-05 14:45:58.907914+00
updated_at2025-11-05 14:45:58.907914+00
descriptionA Rust library that lets you track, cache, and manage commands like a process manager
homepagehttps://github.com/m-epasta/process_state
repositoryhttps://github.com/m-epasta/process_state
max_upload_size
id1918104
size22,761
(m-epasta)

documentation

https://docs.rs/process_state

README

process_state

crates.io docs.rs License

process_state is a lightweight, cross-platform Rust library for tracking, caching, and managing child processes.
It works on both Unix and Windows, making it ideal for build tools, infrastructure management, or any program that launches multiple commands and needs to persist their state.


Features

  • Track any command launched from your Rust program.
  • Persist process state across sessions.
  • Query running or stopped processes.
  • Cross-platform (Windows and Unix).
  • Easy-to-use API with minimal boilerplate.

Installation

Add this to your Cargo.toml:

[dependencies]
process_state = "1.0.0"

Then run:

cargo build

Usage

use process_state::{ProcessState, ProcessStatus};
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize ProcessState
    let mut state = ProcessState::new("myapp")?;

    // Spawn a child process
    let child = if cfg!(windows) {
        Command::new("timeout").arg("5").spawn()?
    } else {
        Command::new("sleep").arg("5").spawn()?
    };

    // Track the process
    state.add_process(&child, "example", "sleep 5")?;

    // Refresh status of all tracked processes
    state.refresh()?;

    // List running processes
    for p in state.get_running() {
        println!("{} is running with PID {}", p.label, p.pid);
    }

    Ok(())
}

API Overview

ProcessState

  • new(namespace: &str) -> Result<ProcessState, Error> – Create a new state manager with a namespace.
  • add_process(&mut self, child: &Child, label: &str, command: &str) -> Result<(), Error> – Track a new process.
  • remove_process(&mut self, pid: u32) -> Result<(), Error> – Stop tracking a process.
  • refresh(&mut self) -> Result<(), Error> – Update all tracked processes’ statuses.
  • get_running(&self) -> Vec<&ProcessInfo> – Get currently running processes.
  • get_all(&self) -> Vec<&ProcessInfo> – Get all tracked processes.

ProcessInfo

  • pid: u32 – Process ID.
  • label: String – Label you assigned.
  • command: String – Original command.
  • start_time: SystemTime – Start time.
  • status: ProcessStatus – Current status.

ProcessStatus

  • Running
  • Stopped
  • Error(String)

License

This project is licensed under MIT OR Apache-2.0.


Contributing

Contributions, bug reports, and feature requests are welcome! Please submit a PR or open an issue on GitHub.

Commit count: 0

cargo fmt