Skip to content

Instantly share code, notes, and snippets.

@levicole
Created August 10, 2017 15:39
Show Gist options
  • Save levicole/9ae7870f75afefb62eb402cf3ae42948 to your computer and use it in GitHub Desktop.
Save levicole/9ae7870f75afefb62eb402cf3ae42948 to your computer and use it in GitHub Desktop.
Watch output of tail in rust
use std::process::{Command, Stdio, ChildStdout};
use std::io::{Read};
use std::thread;
use std::str;
fn main() {
let mut command = Command::new("/usr/bin/tail");
command
.arg("-f")
.arg("/var/log/system.log")
.stdout(Stdio::piped());
if let Ok(child) = command.spawn() {
if let Some(mut out) = child.stdout {
loop {
let mut chars: [u8; 10] = [0; 10];
out.read(&mut chars).expect("didn't work");
print!("{}", str::from_utf8(&chars).unwrap());
}
}
} else {
println!("Process failed to start");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment