从管道中读取一定时间的数据(以秒为单位)

Eli*_*Eli 4 shell pipe stdout

有没有一种简单的方法来创建一个只持续给定壁时间的管道?

我希望能够对在一段时间内写入 STDOUT 的内容(而不是字节或行)进行“快照”。就像是

tail -f /var/log/whatever.log | pipe_for_seconds 10 > ./10_second_sample_of_log

我正在使用 BASH,但如果这在其他一些 shell 中容易得多,我想听听。

mr.*_*tic 6

从 7.0 版开始的Gnu coreutils有一个timeout命令:

timeout 10 tail -f /var/log/whatever.log 
Run Code Online (Sandbox Code Playgroud)

如果你真的需要一个管道来超时一些稍微不同的过程,那么管道进入timeout

tail -f /var/log/whatever.log | timeout 10 cat > ./10_second_sample_of_log
Run Code Online (Sandbox Code Playgroud)

请注意,根据信号和程序行为,杀死管道的某些任意部分可能会因缓冲而导致问题(此问题涵盖相关问题:关闭管道中的缓冲)。它通常也会更改进程的退出代码

如果您没有(最近的)coreutils,这个简单的超时程序也可以很好地运行 http://www.unixlabplus.com/unix-prog/timeout/timeout.html 或 perl 方法:

tail -f /var/log/whatever.log | perl -n -e 'BEGIN{alarm 10; $|=1}; print;'
Run Code Online (Sandbox Code Playgroud)

(注意$|=1关闭输出缓冲,这是为了防止管道中的输出丢失,如上所述。)

(有点古老的)Netpipes包也有一个timelimit命令(你仍然可以在一些 Linux 系统上找到它)。

这个类似的问题还有几个选项:如何为 shell 脚本引入超时?