LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 04-21-2010, 03:44 PM   #1
srons
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 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

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

Rep: Reputation: 97
it's buffer.
http://bytes.com/topic/c/answers/223...dout-buffering
 
Old 04-21-2010, 06:31 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 04-22-2010, 06:43 AM   #4
srons
LQ Newbie
 
Registered: Apr 2010
Location: The Netherlands
Distribution: ubuntu
Posts: 2

Original Poster
Rep: Reputation: 0
Talking

It works, thanks!
 
  


Reply



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 01:54 AM
Redirecting output from screen to console from boot-up ? exceed1 Linux - General 3 11-23-2008 10:26 PM
Redirecting output of running command tommyr1216 Linux - General 3 03-04-2005 08:20 PM
redirecting output of compile to a file halfcan Linux - Hardware 1 02-05-2005 10:01 PM
redirecting cpio screen output to file rawii Solaris / OpenSolaris 1 02-11-2004 09:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:29 AM.

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