【问题标题】:Give the error of command inside timeout在 timeout 内给出命令的错误
【发布时间】:2018-06-02 10:07:23
【问题描述】:

我正在使用 gnome 上的超时功能来等待我的脚本从 inp.txt 读取输入的时间为 1 秒。我需要将脚本的输出写入 out.txt,所以我编写这样的代码

timeout 1 myscript < inp.txt > out.txt

我遇到的问题是,如果 myscript 失败(分段错误等),它会将错误打印到屏幕上。
我想将这些消息保存在 Error.log 之类的内容中,但我不知道如何执行此操作。
我试过了

timeout 1 myscript < inp.txt > out.txt 2> Error.log

但它给了我这个而不是 myscript 的错误。

timeout: the monitored command dumped core

【问题讨论】:

  • 这对我有用,没有输出:(set +m; timeout 5 sleep 5 &gt; out.txt 2&gt; Error.log &amp;); sleep 2; killall -QUIT sleep
  • 谢谢,您的解决方案会在超时后保留消息,这很有用。顺便说一句,添加 --preserve-status 是一个不错的选择,它给了我退出代码,我只需要稍后搜索它。

标签: linux bash shell timeout


【解决方案1】:

使用带有引用参数的子shell 来包装重定向,这样timeout 就看不到它们了:

timeout --preserve-status 1  sh -c 'exec myscript < inp.txt > out.txt 2> Error.log'

【讨论】:

    猜你喜欢
    • 2019-09-08
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    相关资源
    最近更新 更多