Last active
October 1, 2024 17:46
-
-
Save arun1587/0d3f871b1de58501796dc07a93e30ccd to your computer and use it in GitHub Desktop.
Tail file using exec Command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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