Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

./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

migrated from Apr 24 '12 at 3:22

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

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;
printf("%d", *p);
$ make segf
$ bash -c "./segf"
Segmentation fault
$ bash -c "./segf &"
share|improve this answer
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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .