Hi,
i try to read tailed lines of a logfile using this approach:
if env.TailEnabled {
log.Println("setup tail command")
cmd := exec.Command("/usr/bin/tail", "-F", "logfile.log")
log.Println("setup tail pipe")
tailPipe, err := cmd.StdoutPipe()
if err != nil {
log.Fatalln("could not open stdin pipe from tail:", err)
}
log.Println("starting tail pipe scanner")
scanner = bufio.NewScanner(tailPipe)
go func() {
log.Println("starting tail command")
err = cmd.Start()
if err != nil {
log.Fatalln("could not start tail:", err)
}
log.Println("tail pid:", cmd.Process.Pid)
log.Println("waiting for tail to exit")
err = cmd.Wait()
}()
}
log.Println("starting scanner")
for scanner.Scan() {
data := scanner.Bytes()
…
}
Output:
2020/05/16 11:21:11 setup tail command
2020/05/16 11:21:11 setup tail pipe
2020/05/16 11:21:11 starting tail pipe scanner
2020/05/16 11:21:11 rate limiter: &{limit:3000 burst:100 mu:{state:0 sema:0} tokens:0 last:{wall:0 ext:0 loc:<nil>} lastEvent:{wall:0 ext:0 loc:<nil>}}
2020/05/16 11:21:11 starting scanner
2020/05/16 11:21:11 starting tail command
2020/05/16 11:21:11 tail pid: 30823
2020/05/16 11:21:11 waiting for tail to exit
… nothing more
tail is from coreutils 8.25-2ubuntu3~16.04, kernel 4.4.0-1083-aws
It looks like i cannot read lines that way and tail is blocking in write(1, ...
according to strace.
Lsof of tail:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
tail 30693 root cwd DIR 202,1 4096 410091
tail 30693 root rtd DIR 202,1 4096 2 /
tail 30693 root txt REG 202,1 64432 9792 /usr/bin/tail
tail 30693 root mem REG 202,1 1868984 395274 /lib/x86_64-linux-gnu/
libc-2.23.so
tail 30693 root mem REG 202,1 162632 395259 /lib/x86_64-linux-gnu/
ld-2.23.so
tail 30693 root 0r CHR 1,3 0t0 6 /dev/null
tail 30693 root 1w FIFO 0,10 0t0 273545577 pipe
tail 30693 root 2w CHR 1,3 0t0 6 /dev/null
tail 30693 root 3r REG 251,0 14629013611 3276805 /logfile.log
tail 30693 root 4r a_inode 0,11 0 9126 inotify
Lsof of goprog:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
goprog 30688 root cwd DIR 202,1 4096 410091 /service/goprog
goprog 30688 root rtd DIR 202,1 4096 2 /
goprog 30688 root txt REG 202,1 17914743 397448 /service/goprog/goprog
goprog 30688 root 0r CHR 1,3 0t0 6 /dev/null
goprog 30688 root 1w FIFO 0,10 0t0 273482973 pipe
goprog 30688 root 2w FIFO 0,10 0t0 273482973 pipe
goprog 30688 root 3u IPv4 273546843 0t0 TCP redacted
goprog 30688 root 4u a_inode 0,11 0 9126 [eventpoll]
goprog 30688 root 5r FIFO 0,10 0t0 273549055 pipe
goprog 30688 root 6w FIFO 0,10 0t0 273549055 pipe
goprog 30688 root 7u IPv4 273550072 0t0 TCP redacted
goprog 30688 root 8r FIFO 0,10 0t0 273545577 pipe
goprog 30688 root 9u IPv6 273546849 0t0 TCP redacted
goprog 30688 root 14u IPv6 273549061 0t0 TCP *:9290 (LISTEN)
any clues?
Best regards,
Michael