加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 执行超时功能

发布时间:2020-12-15 19:53:37 所属栏目:安全 来源:网络整理
导读:为什么这个工作 timeout 10s echo "foo bar" # foo bar 但这不会 function echoFooBar { echo "foo bar"}echoFooBar # foo bartimeout 10s echoFooBar # timeout: failed to run command `echoFooBar': No such file or directory 我该如何让它工作? timeou
为什么这个工作
timeout 10s echo "foo bar" # foo bar

但这不会

function echoFooBar {
  echo "foo bar"
}

echoFooBar # foo bar

timeout 10s echoFooBar # timeout: failed to run command `echoFooBar': No such file or directory

我该如何让它工作?

timeout是一个命令 – 因此它正在您的bash shell的子进程中执行。因此,它无法访问当前shell中定义的函数。

给定的命令超时作为超时的子进程执行,这是你的shell的一个重要的进程。

你可能会感到困惑,因为echo是一个内置的shell和一个单独的命令。

你可以做的是将你的功能放在自己的脚本文件中,chmod它可执行,然后用超时执行。

或者,fork,在子shell中执行你的功能,在原来的过程中,监视进度,如果需要太长时间,就会杀死子进程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读