Hello All,
I know how to redirect output from one program to a file, for example, when I do:
ping linuxquestions.org > log &
I can observe the contents in real-time, while the program is running with:
tail -f log
But if I create my own little program, like for example:
Code:
#include <stdio.h>
#include <signal.h>
unsigned char stop = 0;
void catch_signal(int sig) {
printf("Signal SIGINT or SIGTERM catched: %d, exiting...\n", sig);
stop = 1;
}
int main(void) {
(void) signal(SIGINT, catch_signal);
(void) signal(SIGTERM, catch_signal);
int i = 0;
while (! stop) {
sleep(1);
fprintf(stdout, "hello %d\n", i);
i++;
}
}
Then with ./a.out > log &, the output is redirected, but the output is only written to log,
after the program is stopped, why?
Why does it work for
ping and not for my own simple C program (compile with gcc without any other options)? How I can I do the same with my program without using a program like
screen.
Thanks the for help,
srons