LinuxQuestions.org
Visit Jeremy's Blog.
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 07-10-2008, 02:48 PM   #1
mfofo
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Rep: Reputation: 0
using pipe with tail -f


I am trying to read the input from stdin, which works fine. However, it does not work when I try to read a pipe from tail -f.
The code:

tail logfile | ./a.out //this one works
but the code:

tail -f logfile | ./a.out //This one does not, it seems like nothing is bein read from the pipe. in my code I am using:

fread(&buffer,1,BUFFER_SIZE,stdin)
 
Old 07-10-2008, 03:23 PM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
The -f option to tail says don't close STDOUT, and just keep reading the file and output forever. Since STDOUT isn't closed, your program's read doesn't complete.

If enough data gets pushed through the pipe, your program will eventually get some data on the read, but not until then.

If you want your program to read a growing file, do it internally, not with an external tool.
 
Old 07-10-2008, 04:17 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
I'm not totally sure on what your trying to acheive but looking at the fact your trying to do a tail -f and then constantly read data there...i may sugest looking at mkfifo

http://linux.die.net/man/3/mkfifo

Take a look it may be what your looking for, this isn't a file though and data written to it won't be stored, its simply a method to pass data from one program to another

Hope it helps
 
Old 07-11-2008, 01:02 AM   #4
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
If you are willing to use Perl, this does exactly what you need: http://search.cpan.org/~mgrabnar/Fil...0.99.3/Tail.pm
 
Old 07-11-2008, 08:05 AM   #5
mfofo
LQ Newbie
 
Registered: Jul 2008
Posts: 2

Original Poster
Rep: Reputation: 0
I figured this out, the problem is in the fread() buffering a lot of the data, so I used read() instead and everything is working fine.
 
Old 07-11-2008, 12:53 PM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Right, but the point is, if you are going to use read(), just read() continually, with a timeout on the read() until the program terminates with a SIGQUIT. Your program is duplicating the behavior of tail -f, so just do it yourself.
 
  


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
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
How to make newer "tail" behave like older "tail" rylan76 Linux - Software 4 12-07-2007 04:27 AM
tail +40 /etc/FILENAME newbie_mel Linux - Software 1 03-20-2006 06:25 AM
How to see that is the HDD doing EXACTLY like tail johnnydangerous Linux - Software 2 03-17-2005 02:52 PM
tail -f dcoder Programming 8 09-01-2003 08:41 PM

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

All times are GMT -5. The time now is 09:05 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