问题
I would like to use the timeout command with an own function, e.g.:
#!/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.
Is there a way to fix this or can timeout not be used on own functions ?
回答1:
timeout doesn't seem to be a built-in command of bash which means it can't access functions. You will have to move the function body into a new script file and pass it to timeout as parameter.
回答2:
timeout requires a command and can't work on shell functions.
Unfortunately your function above has a name clash with the /usr/bin/test executable, and that's causing some confusion, since /usr/bin/test exits immediately. If you rename your function to (say) t, you'll see:
brian@machine:~/$ timeout t
Try `timeout --help' for more information.
which isn't hugely helpful, but serves to illustrate what's going on.
回答3:
One way is to do
timeout 5 bash -c 'sleep 10; echo "done"'
instead. Though you can also hack up something like this:
f() { sleep 10; echo done; }
f & pid=$!
{ sleep 5; kill $pid; } &
wait $pid
回答4:
Provided you isolate your function in a separate script, you can do it this way:
(sleep 1m && killall myfunction.sh) & # we schedule timeout 1 mn here
myfunction.sh
回答5:
Found this question when trying to achieve this myself, and working from @geirha's answer, I got the following to work:
#!/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]}" == "${0}" ]]; then
#timeout func1 after 2 sec, even though it will sleep for 60 sec
timeout 2 bash -c "source $thisfile && func1"
fi
Since timeout executes 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 thisfile variable.
来源:https://stackoverflow.com/questions/11935130/how-to-use-timeout-command-with-a-own-function