I would like to run a process and process the standard output of it while the process is running, but I'm not sure how to do it.
fn execute_command(&mut self){
let (exec_cmd, cmd_args) = self.command_as_arguments.split_at(1);
let mut cmd_output = Command::new(exec_cmd[0]);
// std::process::Command = cmd_output
for arg in cmd_args{
cmd_output.arg(arg);
}
// let std_out_handle = cmd_output.stdout(Stdio::null());
let std_out_handle = cmd_output.stdout(Stdio::piped());
// let std_out_handle = cmd_output.stdout(Stdio::inherit());
let ok_cprocess_handle = std_out_handle.spawn().unwrap();
//process::Child = ok_cprocess_handle
let mut ok_cprocess_handle_stdout = ok_cprocess_handle.stdout.unwrap();
// println!("CHILD STDOUT={:?}",ok_cprocess_handle_stdout);
let mut output_string = String::new();
ok_cprocess_handle_stdout.read_to_string(&mut output_string);
println!("CHILD STRING STDOUT={:?}",output_string);
let mut output_vec = Vec::new();
ok_cprocess_handle_stdout.read_to_end(&mut output_vec);
println!("CHILD VEC STDOUT={:?}",output_vec);
}
I edited and reduced the code, but when I run the program I get still an empty string if I try to read the output: ok_cprocess_handle_stdout.read_to_string(&mut output_string);
println!("CHILD STRING STDOUT={:?}",output_string);
I am expecting read some output at that point, but what am I missing?