Browse Source

Also capture stderr

master
Julio Biason 9 months ago
parent
commit
14554010fc
  1. 15
      commandtest/src/main.rs
  2. 6
      commandtest/src/the_script.sh

15
commandtest/src/main.rs

@ -1,6 +1,6 @@
use std::ffi::OsString; use std::ffi::OsString;
use std::fs::File; use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::process::Command; use std::process::Command;
@ -25,10 +25,13 @@ fn main() {
let mut cmd = Command::new(bash) let mut cmd = Command::new(bash)
.arg(the_script) .arg(the_script)
.stdout(std::process::Stdio::piped()) .stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn() .spawn()
.unwrap(); .unwrap();
let stdout = cmd.stdout.take().unwrap(); let stdout = cmd.stdout.take().unwrap();
let mut stderr = cmd.stderr.take().unwrap();
let writer_pid = std::thread::spawn(move || { let writer_pid = std::thread::spawn(move || {
let reader = BufReader::new(stdout); let reader = BufReader::new(stdout);
let lines = reader.lines(); let lines = reader.lines();
@ -55,5 +58,13 @@ fn main() {
cmd.wait().unwrap(); cmd.wait().unwrap();
let warnings = writer_pid.join().unwrap(); let warnings = writer_pid.join().unwrap();
let mut buffer = String::new();
stderr.read_to_string(&mut buffer).unwrap();
let mut file = OpenOptions::new().append(true).open("script.log").unwrap();
file.write(buffer.as_bytes()).unwrap();
println!("Warnings:\n{:?}", warnings); println!("Warnings:\n{:?}", warnings);
println!("ERR:\n{:?}", buffer)
} }

6
commandtest/src/the_script.sh

@ -13,6 +13,12 @@ do
echo " And keeps going till there are no more spaces-prefixes" echo " And keeps going till there are no more spaces-prefixes"
fi fi
if (( $loop%8 == 0)); then
# ERR is just to make sure we find it easily in the logs
echo "ERR: Sometimes, I also write in stderr!" >&2
echo "ERR: Just for funsies!" >&2
fi
echo "Like this." echo "Like this."
echo "Then you're good to go." echo "Then you're good to go."
echo "" echo ""

Loading…
Cancel
Save