如何在Bash命令中设置超时并计算输出到标准输出的行数?

7

我想要做类似于 timeout 12s tail -f access.log | wc -l 这样的操作,但是我没有看到来自 wc 的输出。需要做些什么才能完成这个任务?

1个回答

5

使用--foreground选项和timeout一起使用:

timeout --foreground 12s tail -f access.log | wc -l

根据man timeout

--foreground   when not running timeout directly from a shell prompt,
               allow COMMAND to read from the TTY and get TTY signals;
               in this mode, children of COMMAND will not be timed out

在初始尝试时一定要等待足够长的时间。例如,我最初使用了 timeout --foreground 1s,但该命令花费的时间远远超过了1秒钟,可能是因为需要处理的数据量很大。 - Noel Yap

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接