Vincent Lefevre wrote:
Not every system has bash. If this is for compatibility, you can learn POSIX sh, but e.g. Solaris /bin/sh is not a POSIX sh. For this reason and because POSIX sh is limited (you can't execute a command and have a timeout on it),
Here is a method for doing a timeout. I'm not arguing against the claim that sh is limited, nor am I claiming that the method presented here is robust (it doesn't work well if the function has already completed, for example), but this certainly demonstrates that it is possible to execute a command with a timeout. #!/bin/sh # A function which never completes. foo() { echo "Never returns! $$" while true; do sleep 1; done } # Execute the commands with the specified timeout. timeout() { local timeout=$1 shift local cmd=$@ echo $cmd $cmd & sleep $timeout kill %% } # Execute foo, but terminate it after 1 second. timeout 1 foo