Seeking for a crate to run other cli executables

Hey guys , now I'm trying to run another cli executables in my rust program, and I hope to catch output every time when the executable print new things, instead of geting all the output when It ends.

Doesn't std::process::ChildStdout provide that functionality?


Thanks for help, I will try it tomorrow

Any examples ?

I don't know how to catch its output once a while

You can't "check once in a while" in the thread you're running in, because the stream is blocking. The first answer in this SO post explains this and has an example of creating a thread to read the output. The second answer shows how to do this using Tokio, which applies if you happen to be already using Tokio.


Thanks for your help, it solved now.

use std::io::prelude::*;
use std::process::{Command, Stdio};
use std::thread::{self, sleep};
use std::time::Duration;

fn main() {
    let mut child = Command::new("./target/debug/out.exe")
        // .stdin(Stdio::piped())

    let mut stdout = child.stdout.take().unwrap();

    let handle = thread::spawn(move || loop {

        let mut buf = Vec::new();
        buf.resize(1000, 0);

        let got = buf).unwrap();
        buf.resize(got, 0);

        print!("{}", String::from_utf8(buf).unwrap());

    let exit = child.wait().unwrap();


1 Like

You're welcome!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.