Silencing Subprocess Output

Had some odd behavior I thought I would ask about. I was making a check to ensure the ssh-agent is running before other code.

fn main() {
    use std::process::Command;

    let status = Command::new("ssh-add")
                     .arg("-l")
                     .status()
                     .expect("failed to execute process");

    if status.success() {
        println!("The ssh-agent is not installed, running or configured correctly.");
        println!("Try running \"eval $(ssh-agent -s); ssh-add;\" or configuring .bash_profile");
    }
}

The check itself works fine, but I wanted to silence the output from the subprocess and only print what I have with println! or nothing. I decided to just add ">/dev/null" to the end of my command which works perfect from the Linux command-line. However, when the ssh-agent is running, I get output still instead of nothing:

 $ ./target/release/system 
3072 SHA256:xxxxxxxxxxxxxxxxxxxxxxxirwZSnL8jKRrA/xxxxx user@box (RSA)

Obviously not so many 'x's, but you get the point. If I can't do this from the command-line is there another way to do this with Rust? Maybe my google-fu is getting weak, but when I looked for this I couldn't find much relatable.

Thanks!

Try this. It might be that its outputting to stderr

fn main() {
    use std::process::Command;

    let status = Command::new("ssh-add")
                     .arg("-l")
                     .stdout(std::process:Stdio::null())
                     .stderr(std::process:Stdio::null())
                     .status()
                     .expect("failed to execute process");

    if status.success() {
        println!("The ssh-agent is not installed, running or configured correctly.");
        println!("Try running \"eval $(ssh-agent -s); ssh-add;\" or configuring .bash_profile");
    }
}

Hmm... I tried that, but got a lot of errors:

$ cargo build --release
   Compiling system v0.1.0 (/home/user/rust/system)
error[E0423]: expected value, found module `std::process`
 --> src/main.rs:6:30
  |
6 |                      .stdout(std::process:Stdio::null())
  |                              ^^^^^^^^^^^^- help: maybe you meant to write a path separator here: `::`
  |                              |
  |                              not a value

error[E0423]: expected value, found module `std::process`
 --> src/main.rs:7:30
  |
7 |                      .stderr(std::process:Stdio::null())
  |                              ^^^^^^^^^^^^- help: maybe you meant to write a path separator here: `::`
  |                              |
  |                              not a value

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/main.rs:6:50
  |
6 |                      .stdout(std::process:Stdio::null())
  |                                                  ^^^^^^ only `Fn` traits may use parentheses

error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
 --> src/main.rs:7:50
  |
7 |                      .stderr(std::process:Stdio::null())
  |                                                  ^^^^^^ only `Fn` traits may use parentheses

error[E0223]: ambiguous associated type
 --> src/main.rs:6:43
  |
6 |                      .stdout(std::process:Stdio::null())
  |                                           ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<std::process::Stdio as Trait>::null`

error[E0223]: ambiguous associated type
 --> src/main.rs:7:43
  |
7 |                      .stderr(std::process:Stdio::null())
  |                                           ^^^^^^^^^^^^^ help: use fully-qualified syntax: `<std::process::Stdio as Trait>::null`

error: aborting due to 6 previous errors

Some errors have detailed explanations: E0214, E0223, E0423.
For more information about an error, try `rustc --explain E0214`.
error: could not compile `system`.

I tried changing "use std::process::Command;" to "use std::process::{Command, Stdio};", but got the same results. Would there possibly be another use statement I need?

Sorry thats a typo by me. Try this

fn main() {
    use std::process::{Command, Stdio};

    let status = Command::new("ssh-add")
                     .arg("-l")
                     .stdout(Stdio::null())
                     .stderr(Stdio::null())
                     .status()
                     .expect("failed to execute process");

    if !status.success() {
        println!("The ssh-agent is not installed, running or configured correctly.");
        println!("Try running \"eval $(ssh-agent -s); ssh-add;\" or configuring .bash_profile");
    }
}

Edit:
Fixed success if statement

1 Like

Totally understandable. You also forgot the "!" symbol in front of status.success(). Not complaining, but fyi for anyone who may be reading this. Once I corrected that this ran as expected without subprocess output. Thank you again!!!