This small Rust library simplifies the process of running system commands by
spawning a new child process and reading the content of either the stdout
or stderr stream handle.
- Add the crate to your project
cargo add popen_rs
- Import the popen_rs crate to your source file
use popen_rs::Popen;
Spawns a new child process and captures the handles of stdout and stderr. This function will return the content of either stdout or stderr.
pub fn spawn(&mut self) -> io::Result<String>
pub fn pid(&mut self) -> Option<u32>
use popen_rs::Popen;
fn main() {
let command = "ls -l";
let mut process = Popen::new(command);
match process.spawn() {
Ok(output) => {
let pid = process.pid().unwrap();
println!("Output of `{}` [PID:{}]:", command, pid);
println!("\n{}", output);
},
Err(err) => eprintln!("Error: {}", err),
}
}
This project is published under the MIT License.