5

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?

0
1

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 &"
$
1
  • 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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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