Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-21-2010, 04:44 PM   #1
LQ Newbie
Registered: Apr 2010
Location: The Netherlands
Distribution: ubuntu
Posts: 2

Rep: Reputation: 0
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 > 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:
#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) {
        fprintf(stdout, "hello %d\n", 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,


Last edited by srons; 04-21-2010 at 04:54 PM.
Old 04-21-2010, 05:15 PM   #2
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
it's buffer.
Old 04-21-2010, 07:31 PM   #3
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,589

Rep: Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433Reputation: 2433
BTW, the '&' is redundant. Re-direction works anyway eg

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


./myprog >myprog.log 2>&1

The '&' you used at the end simply puts the prog in the background, which is a separate issue.
Old 04-22-2010, 07:43 AM   #4
LQ Newbie
Registered: Apr 2010
Location: The Netherlands
Distribution: ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0

It works, thanks!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
awk: how can I print out a message to the screen when redirecting the output to file. quanba Programming 8 07-13-2015 02:54 AM
Redirecting output from screen to console from boot-up ? exceed1 Linux - General 3 11-23-2008 11:26 PM
Redirecting output of running command tommyr1216 Linux - General 3 03-04-2005 09:20 PM
redirecting output of compile to a file halfcan Linux - Hardware 1 02-05-2005 11:01 PM
redirecting cpio screen output to file rawii Solaris / OpenSolaris 1 02-11-2004 10:28 AM > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration