Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a shell function with timeout

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?

like image 227
speendo Avatar asked Mar 31 '12 09:03

speendo


People also ask

What is timeout in shell script?

timeout is a command-line utility that runs a specified command and terminates it if it is still running after a given period of time. In other words, timeout allows you to run a command with a time limit.

How do you delay a shell script?

/bin/sleep is Linux or Unix command to delay for a specified amount of time. You can suspend the calling shell script for a specified time. For example, pause for 10 seconds or stop execution for 2 mintues. In other words, the sleep command pauses the execution on the next shell command for a given time.

How do I sleep in bash?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.

How timeout is implemented in Linux?

Run Linux Commands Using the timeout Tool Its syntax is as follows. To use the command, you specify a timeout value (in seconds) with the command you want to run. For instance, to timeout a ping command after 5 seconds, you can run the following command. You do not have to specify the (s) after number 5.


1 Answers

As Douglas Leeder said you need a separate process for timeout to signal to. Workaround by exporting function to subshells and running subshell manually.

export -f echoFooBar timeout 10s bash -c echoFooBar 
like image 99
user3132194 Avatar answered Sep 22 '22 15:09

user3132194