问题描述
我有一个启动一个子进程崩溃(实际上,挂起)不时与没有明显的原因bash脚本(封闭源代码,所以没有太多我可以做些什么)。因此,我希望能够推出这个过程给定的时间量,并杀死它,如果它没有一个特定的时间量之后成功返回。
I have a bash script that launches a child process that crashes (actually, hangs) from time to time and with no apparent reason (closed source, so there isn't much I can do about it). As a result, I would like to be able to launch this process for a given amount of time, and kill it if it did not return successfully after a given amount of time.
有一个简单和强大的的方式来实现这一目标使用bash?
Is there a simple and robust way to achieve that using bash?
P.S:告诉我,如果这个问题更适合于serverfault或超级用户。
P.S.: tell me if this question is better suited to serverfault or superuser.
推荐答案
的(曾经出现在:
)的
如果你不介意下载使用成才暂停
(命令和apt-get安装超时
),并用它这样的:
If you don't mind downloading someting use timeout
(sudo apt-get install timeout
) and use it like:
timeout 10 ping www.goooooogle.com
如果你不想下载的东西做什么超时内部所做的:
If you don't want to download something do what timeout does internally:
( cmdpid=$BASHPID; (sleep 10; kill $cmdpid) & exec ping www.goooooogle.com )
在您要为更多的bash code做了超时,使用第二个选项这样的情况:
In the case that you want to do a timeout for more bash code, use the second option as such:
( cmdpid=$BASHPID;
(sleep 10; kill $cmdpid) \
& while ! ping -w 1 www.goooooogle.com
do
echo crap;
done )
这篇关于bash脚本,在给定超时之后杀死一个子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!