与超时执行功能 [英] execute function with timeout
问题描述
为什么将这项工作
timeout 10s echo "foo bar" # foo bar
但这不会
function echoFooBar {
echo "foo bar"
}
echoFooBar # foo bar
timeout 10s echoFooBar # timeout: failed to run command `echoFooBar': No such file or directory
和我怎样才能使它工作?
and how can I make it work?
推荐答案
暂停
是一个命令 - 所以它在你的bash shell的子进程执行。因此,它必须在当前的shell定义你的函数没有访问。
timeout
is a command - so it is executing in a subprocess of your bash shell. Therefore it has no access to your functions defined in your current shell.
命令暂停
给定为超时的子进程执行 - 你的shell的盛大子进程
The command timeout
is given is executed as a subprocess of timeout - a grand-child process of your shell.
您可能会感到困惑,因为回声
既是一个shell内置和独立的命令。
You might be confused because echo
is both a shell built-in and a separate command.
您可以做的就是把你的函数在它自己的脚本文件是什么,通过chmod将其视为可执行文件,然后用暂停
。
What you can do is put your function in it's own script file, chmod it to be executable, then execute it with timeout
.
另外叉,执行你的函数在一个子shell - 并在原工艺,监控进度,杀子,如果时间过长
Alternatively fork, executing your function in a sub-shell - and in the original process, monitor the progress, killing the subprocess if it takes too long.
这篇关于与超时执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!