如何在 Bash 中使一组命令超时 [英] How to timeout a group of commands in Bash

查看:24
本文介绍了如何在 Bash 中使一组命令超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在摆弄 Linux 命令超时":它只是在给定几秒钟后停止长时间运行的命令.但我想超时的不仅是一个命令,而是一组命令.我可以通过两种方式 ( ) 和 { ;} 对命令进行分组,但是以下都不起作用:

I am fiddling with Linux command "timeout": it simply stops a long running command after a given seconds. But I would like to timeout not only a command, but a group of commands. I can group command in two way ( ) and { ;} however none of the following works:

timeout 1 { sleep 2; echo something; }
timeout 1 ( sleep 2; echo something )

如何通过分组来做到这一点?

How can I do that with grouping?

推荐答案

timeout 不是 shell 实用程序,它不进行 shell 样式的处理.必须给它一个命令才能执行.但是,该命令可以有任意数量的参数.幸运的是,你可以给它的命令之一是 bash:

timeout is not a shell utility and it does not do shell-style processing. It must be given one single command to execute. That command, though, can have any number of arguments. Fortunately, one of the commands that you can give it is bash:

timeout 1 bash -c '{ sleep 2; echo something; }'

当然,在这种形式下,大括号现在是多余的:

Of course, in this form, the braces are now superfluous:

timeout 1 bash -c 'sleep 2; echo something'

这里,bashtimeout 执行的命令.-csleep 2;echo something 是该命令的参数.

Here, bash is the command that timeout executes. -c and sleep 2; echo something are argument to that command.

这篇关于如何在 Bash 中使一组命令超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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