Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect stdout of a killed executable

I have a C file that looks like this:

#include <stdio.h>

int main(void)
{
   printf("Hello world\n");
   while (1);
   return 0;
}

And I would like to see "Hello world" printed in a new file. But it doesn't work when I try to redirect the standard output like this:

./hello_world >> logfile &

And then kill the program hello_world.

like image 464
Zah Avatar asked Dec 03 '25 07:12

Zah


1 Answers

You need to flush stdout before the loop:

#include <stdio.h>

int main(void)
{
   printf("Hello world\n");
   fflush(stdout);
   while (1);
   return 0;
}
like image 120
Adam Zalcman Avatar answered Dec 05 '25 21:12

Adam Zalcman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!