为什么`timeout`不适用于管道? [英] Why does `timeout` not work with pipes?

查看:132
本文介绍了为什么`timeout`不适用于管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的timeout命令行调用(仅出于测试原因没有意义)无法按预期方式工作.它等待10秒钟,并且3秒钟后不停止命令的运行.为什么?

The following command line call of timeout (which makes no sense, just for testing reason) does not work as expected. It waits 10 seconds and does not stop the command from working after 3 seconds. Why ?

timeout 3 ls | sleep 10

推荐答案

您的命令正在运行timeout 3 ls,并将其输出传递到sleep 10.因此,sleep命令不受timeout的控制,它将始终睡眠10秒钟.

What your command is doing is running timeout 3 ls and piping its output to sleep 10. The sleep command is therefore not under the control of timeout and will always sleep for 10s.

这样的效果会达到预期的效果.

Something like this would give the desired effect.

timeout 3 bash -c "ls | sleep 10"

这篇关于为什么`timeout`不适用于管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆