bash 如何使用具有自己功能的超时命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11935130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use timeout command with a own function?
提问by John Threepwood
I would like to use the timeout command with an own function, e.g.:
我想将 timeout 命令与自己的功能一起使用,例如:
#!/bin/bash
function test { sleep 10; echo "done" }
timeout 5 test
But when calling this script, it seems to do nothing. The shell returns right after I started it.
但是在调用这个脚本时,它似乎什么都不做。shell 在我启动后立即返回。
Is there a way to fix this or can timeout not be used on own functions ?
有没有办法解决这个问题,或者超时不能用于自己的功能?
回答by Aaron Digulla
timeoutdoesn't seem to be a built-in command of bashwhich means it can't access functions. You will have to move the function body into a new script file and pass it to timeoutas parameter.
timeout似乎不是内置命令,bash这意味着它无法访问功能。您必须将函数体移动到一个新的脚本文件中并将其timeout作为参数传递给。
回答by Brian Agnew
timeoutrequires a command and can't work on shell functions.
timeout需要一个命令,不能在 shell 函数上工作。
Unfortunately your function above has a name clash with the /usr/bin/testexecutable, and that's causing some confusion, since /usr/bin/testexits immediately. If you rename your function to (say) t, you'll see:
不幸的是,你上面的函数与/usr/bin/test可执行文件有名称冲突,这会引起一些混乱,因为它会/usr/bin/test立即退出。如果您将函数重命名为 (say) t,您将看到:
brian@machine:~/$ timeout t
Try `timeout --help' for more information.
which isn't hugely helpful, but serves to illustrate what's going on.
这不是很有帮助,但有助于说明正在发生的事情。
回答by geirha
One way is to do
一种方法是做
timeout 5 bash -c 'sleep 10; echo "done"'
instead. Though you can also hack up somethinglike this:
反而。虽然你也可以破解这样的东西:
f() { sleep 10; echo done; }
f & pid=$!
{ sleep 5; kill $pid; } &
wait $pid
回答by Kinnin Vo-Shay
Found this question when trying to achieve this myself, and working from @geirha's answer, I got the following to work:
在自己尝试实现此问题时发现了这个问题,并根据@geirha 的回答进行了操作,我得到了以下结果:
#!/usr/bin/env bash
# "thisfile" contains full path to this script
thisfile=$(readlink -ne "${BASH_SOURCE[0]}")
# the function to timeout
func1()
{
echo "this is func1";
sleep 60
}
### MAIN ###
# only execute 'main' if this file is not being source
if [[ "${BASH_SOURCE[0]}" == "(sleep 1m && killall myfunction.sh) & # we schedule timeout 1 mn here
myfunction.sh
" ]]; then
#timeout func1 after 2 sec, even though it will sleep for 60 sec
timeout 2 bash -c "source $thisfile && func1"
fi
Since timeoutexecutes the command its given in a new shell, the trick was getting that subshell environment to source the script to inherit the function you want to run. The second trick was to make it somewhat readable..., which led to the thisfilevariable.
由于timeout在新 shell 中执行给定的命令,所以诀窍是让该子 shell 环境获取脚本以继承您想要运行的函数。第二个技巧是让它有点可读......,这导致了thisfile变量。
回答by Stephane Rouberol
Provided you isolate your function in a separate script, you can do it this way:
如果您在单独的脚本中隔离您的函数,您可以这样做:
##代码##
