Skip to content

Instantly share code, notes, and snippets.

@arun1587
Last active October 1, 2024 17:46
Show Gist options
  • Select an option

  • Save arun1587/0d3f871b1de58501796dc07a93e30ccd to your computer and use it in GitHub Desktop.

Select an option

Save arun1587/0d3f871b1de58501796dc07a93e30ccd to your computer and use it in GitHub Desktop.
Tail file using exec Command
package main
import (
"bufio"
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("tail", "-f", "./test.log")
reader, err := cmd.StdoutPipe()
if err != nil {
return
}
scanner := bufio.NewScanner(reader)
go func() {
for scanner.Scan() {
line := scanner.Text()
fmt.Printf("%s\n", line)
}
}()
err = cmd.Start()
if err != nil {
return
}
err = cmd.Wait()
if err != nil {
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment