Why would this work
timeout 10s echo "foo bar" # foo bar
but this wouldn't
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?
This function uses only builtins
Maybe consider evaling "$*" instead of running $@ directly depending on your needs
It starts a job with the command string specified after the first arg that is the timeout value and monitors the job pid
It checks every 1 seconds, bash supports timeouts down to 0.01 so that can be tweaked
Also if your script needs stdin,
read
should rely on a dedicated fd (exec {tofd}<> <(:)
)Also you might want to tweak the kill signal (the one inside the loop) which is default to
-15
, you might want-9
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.The command
timeout
is given is executed as a subprocess of timeout - a grand-child process of your shell.You might be confused because
echo
is both a shell built-in and a separate command.What you can do is put your function in it's own script file, chmod it to be executable, then execute it with
timeout
.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.
You can create a function which would allow you to do the same as timeout but also for other functions:
And could run as below:
Note: The solution came from one of my questions: Elegant solution to implement timeout for bash commands and functions
if you just want to add timeout as an additional option for the entire existing script, you can make it test for the timeout-option, and then make it call it self recursively without that option.
example.sh:
running this script without timeout:
running it with a one minute timeout:
As Douglas Leeder said you need a separate process for timeout to signal to. Workaround by exporting function to subshells and running subshell manually.