snippetrustCritical
How do I invoke a system command and capture its output?
Viewed 0 times
capturehowoutputandinvokeitscommandsystem
Problem
Is there a way to invoke a system command, like
ls or fuser in Rust? How about capturing its output?Solution
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 details
output— runs the program and returns the output
status— runs the program and returns the exit code
One simple example from the docs:
use std::process::Command;
Command::new("ls")
.arg("-l")
.arg("-a")
.spawn()
.expect("`ls` should be executable");Code Snippets
use std::process::Command;
Command::new("ls")
.arg("-l")
.arg("-a")
.spawn()
.expect("`ls` should be executable");Context
Stack Overflow Q#21011330, score: 229
Revisions (0)
No revisions yet.