LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   redirecting output, observe with tail -f <file>, while running, without screen? (https://www.linuxquestions.org/questions/linux-newbie-8/redirecting-output-observe-with-tail-f-file-while-running-without-screen-803368/)

srons 04-21-2010 03:44 PM

redirecting output, observe with tail -f <file>, while running, without screen?
 
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

troop 04-21-2010 04:15 PM

it's buffer.
http://bytes.com/topic/c/answers/223...dout-buffering

chrism01 04-21-2010 06:31 PM

BTW, the '&' is redundant. Re-direction works anyway eg

./myprog >myprog.out 2>myprog.err

OR

./myprog >myprog.log 2>&1

The '&' you used at the end simply puts the prog in the background, which is a separate issue.

srons 04-22-2010 06:43 AM

It works, thanks! :)


All times are GMT -5. The time now is 03:53 PM.