# unistore-process
UniStore 进程管理能力,提供异步子进程创建和管理功能。
## 概述
`unistore-process` 提供:
- 异步进程创建
- 标准输入/输出/错误流管理
- 进程生命周期管理
- 优雅终止与强制终止
- 环境变量配置
- 工作目录设置
- 与 UniStore 生命周期集成
## 安装
```toml
[dependencies]
unistore-process = "0.1"
```
## 快速开始
```rust
use unistore_process::{Command, shell};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// 简单命令执行
let output = Command::new("echo")
.arg("Hello, World!")
.output()
.await?;
println!("stdout: {}", output.stdout_lossy());
// 使用 shell 执行
let output = shell("echo Hello from shell").await?;
println!("stdout: {}", output.stdout_lossy());
Ok(())
}
```
## 命令构建器
```rust
let output = Command::new("cargo")
.args(["build", "--release"])
.current_dir("/path/to/project")
.env("RUST_LOG", "debug")
.output()
.await?;
if output.success() {
println!("构建成功!");
} else {
eprintln!("构建失败: {}", output.stderr_lossy());
}
```
## 后台进程
```rust
use std::time::Duration;
// 启动后台进程
let mut child = Command::new("my-server")
.spawn()
.await?;
println!("进程 ID: {}", child.id());
// 等待完成或超时
match child.wait_timeout(Duration::from_secs(30)).await? {
Some(status) => println!("进程退出: {:?}", status),
None => {
println!("超时,终止进程");
child.terminate().await?;
}
}
```
## 便捷函数
```rust
use unistore_process::{shell, exec, which};
// 执行 shell 命令
let output = shell("ls -la").await?;
// 执行命令并获取标准输出
let version = exec("rustc", &["--version"]).await?;
// 检查命令是否存在
if let Some(path) = which("cargo").await {
println!("cargo 位于: {}", path.display());
}
```
## 进程控制
```rust
// 优雅终止(发送 SIGTERM / TerminateProcess)
child.terminate().await?;
// 强制终止(发送 SIGKILL / TerminateProcess)
child.kill().await?;
```
## 平台支持
| Windows | ✅ |
| Linux | ✅ |
| macOS | ✅ |
## 许可证
MIT OR Apache-2.0
## 致谢
本 crate 基于以下优秀项目构建:
- [tokio](https://github.com/tokio-rs/tokio) - 异步运行时及进程管理
感谢 Tokio 团队提供的出色异步基础设施!