Is there a way to invoke a system command, like ls
or fuser
in Rust? How about capturing its output?
Is there a way to invoke a system command, like ls
or fuser
in Rust? How about capturing its output?
std::process::Command
allows for that.
There are multiple ways to spawn a child process and execute an arbitrary command on the machine:
spawn
— runs the program and returns a value with detailsoutput
— runs the program and returns the outputstatus
— runs the program and returns the exit codeOne simple example from the docs:
use std::process::Command;
Command::new("ls")
.arg("-l")
.arg("-a")
.spawn()
.expect("ls command failed to start");