【问题标题】:Check for stdout or stderr检查标准输出或标准错误
【发布时间】:2010-02-24 20:23:33
【问题描述】:

我在我的 shell 脚本中使用的一个二进制文件导致了分段错误(返回值:139)

尽管我将 stdout 和 stderr 都重定向到日志文件,但当我运行 shell 脚本时,Segmentation Fault 错误消息仍会显示在终端中。

是否可以将此消息从 Segfault 重定向到日志文件??

【问题讨论】:

    标签: bash shell segmentation-fault stdout stderr


    【解决方案1】:

    您看到的分段错误消息是由运行您的程序的 shell 打印的。这种行为因外壳而异,因此您可以尝试一些事情(如果您坚持将分段错误消息从外壳重定向中获取到您的日志中)。

    # Have sh invoke your program, and redirect output from both sh and your program into logfile
    sh -c "program arguments more arguments" >logfile 2>&1
    # Force bash to not just exec your program (/bin/true part), and redirect output
    # from both bash and your program into logfile
    bash -c "/bin/true; program arguments more arguments" >logfile 2>&1
    

    【讨论】:

    • 简洁优雅。 +1
    【解决方案2】:

    好吧,我正在回答我自己的问题.. :) 我在这里找到了答案 How can I suppress the output due to a SIGSEGV or a SIGFPE in a Fortran 90 program?

    诀窍是添加

    `exec 2> filename`  
    

    在 shell 脚本中。

    这会将所有消息从 shell 重定向到日志文件

    【讨论】:

      【解决方案3】:

      试试

      ./program &> logfile
      

      I/O 重定向有各种例子here,看看

      你也可以看看这个discussion

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多