Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I ran an executable in bash

./code > log

It shows occasional error messages on terminal whereas all printf statements go into log file. I re-run it like below

./code >& log

Now, the occasional error messages also go into log. But if there is a segmentation fault, it is still shown on terminal. Why? How to make the message Segmentation fault (core dumped) go into the log file?


user$ bash --version

GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)

share|improve this question
add comment

2 Answers

up vote 9 down vote accepted

A segmentation fault is a signal, if you are not catching this then your program will be terminated and your shell will print this to its stderr (rather than your program's stderr).

It is possible for either your program or the shell to take specific actions when this occurs, either by the program catching the signal or your shell trapping the SIGCHILD signal and then checking your child's exit status.

share|improve this answer
    
Thanks! How would I trap it in shell script? I want to run some other commands depending on whether seg-fault has occurred or not. How would one put an if condition using this? –  user13107 Oct 30 '12 at 7:54
1  
@user13107 help trap –  Carlos Campderrós Oct 30 '12 at 8:31
2  
yup. got it. if anyone's interested, here's what i did pastebin.com/QyeJYYHC –  user13107 Oct 30 '12 at 9:50
    
I down voted, because you can't catch segfault signal –  warl0ck Oct 31 '12 at 7:31
    
The shell trap command traps signals sent to the shell. So it won't work to catch the one being sent to your program. –  derobert Oct 31 '12 at 11:36
show 4 more comments

The “segmentation fault” message is printed to stderr, but it's the shell's standard error, not the program's standard error. The shell prints this message when it detects that the program has terminated due to a signal.

You can silence the message by redirecting stderr around the part of the shell script that runs the program:

{ ./code; } >&log
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.