Execute a shell function with timeout

前端 未结 10 1128
死守一世寂寞
死守一世寂寞 2020-12-01 00:40

Why would this work

timeout 10s echo \"foo bar\" # foo bar

but this wouldn\'t

function echoFooBar {
  echo \"foo bar\"
}

e         


        
10条回答
  •  没有蜡笔的小新
    2020-12-01 01:26

    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.

提交回复
热议问题