Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am debugging an application that has to run with other components. I am wrapping everything in a script:

#!/bin/bash 
./component1 > 1.log & 
./my_application & 
./component2 > 2.log &

I want to see the output in the terminal so I didn't redirect the output of my_application.

Turned out my_application had a segfault. But the weird thing is that the output line that complains about the segmentation fault information is not printing out in my terminal. That very last line is simply missing.

If I run ./my_appliation alone in another terminal, then the output works fine. I see the last line of "Segmentation fault"

Why is my output missing when I run my application with & ? What difference does it make if I add & to the end of the command?

share|improve this question
add comment

migrated from stackoverflow.com Apr 24 '12 at 3:22

This question came from our site for professional and enthusiast programmers.

2 Answers

up vote 1 down vote accepted

The message "Segmentation fault" is not really written from ./my_applitation but from the shell.

When you use & after a command, the shell will run it in background in a subshell, I think the stderr output of that subshell is what is being lost.

I confirm the same thing here:

$ cat >segf.c <<EOF
int main(int argc, char *argv[]){
char *p;
p=0;
printf("%d", *p);
}
EOF
$ make segf
$ bash -c "./segf"
Segmentation fault
$ bash -c "./segf &"
$
share|improve this answer
2  
An additional consideration in your example is that, because the parent process of segf has exited, its parent has become init. On some systems, init may syslog if it reaps a child which died by signal, but on most it does not; in any case, it has no way to report the failure on the terminal that started the dead process. This is the missing piece in the original question as well: the shell that ran the script has exited, so my_application's parent is init. –  geekosaur Apr 24 '12 at 4:48
add comment

Maybe, the reason is that ./component1, ./my_application, ./component2 executes simultaneously in your script. If they uses the same files or devices in your system, the segfault can be appeared.

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.