Redirect output of child process spawned from Rust

Viewed 2921

I need to redirect output of a spawned child process. This is what I tried but it doesn't work:

Command::new(cmd)
    .args(&["--testnet",
            "--fast",
            format!("&>> {}", log_path).as_str()])
    .stdin(Stdio::piped())
    .stdout(Stdio::inherit())
    .spawn()
3 Answers

They have added a new (in 7/6/2017 https://github.com/rust-lang/rust/pull/42133) std::process::Stdio::from function that also accepts files and works on both windows and unix systems. example:

use std::fs::File;
use std::process::Command;

fn main() {
    let f = File::create("path/to/some/log.log").unwrap();
    let mut cmd = Command::new("some command")
        .stdout(std::process::Stdio::from(f))
        .spawn()
        .unwrap();
    cmd.wait().unwrap();
}
Related