LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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, 04: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 04:54 PM.
 
Old 04-21-2010, 05:15 PM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 96
it's buffer.
http://bytes.com/topic/c/answers/223...dout-buffering
 
Old 04-21-2010, 07:31 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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, 07: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


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


All times are GMT -5. The time now is 02:06 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration