Execute a shell function with timeout

2019-01-11 02:28发布

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?

7条回答
神经病院院长
2楼-- · 2019-01-11 03:16

There's an inline alternative also launching a subprocess of bash shell:


timeout 10s bash <<EOT
function echoFooBar {
  echo foo
}

echoFooBar
sleep 20
EOT

查看更多
登录 后发表回答