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?

CuriousMind
  • 2,021

2 Answers2

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 &"
$
elias
  • 158
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.