Help answer threads with 0 replies.
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 07-10-2008, 03:48 PM   #1
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:

Old 07-10-2008, 04:23 PM   #2
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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, 05:17 PM   #3
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

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, 02:02 AM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
If you are willing to use Perl, this does exactly what you need:
Old 07-11-2008, 09:05 AM   #5
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, 01:53 PM   #6
Mr. C.
Senior Member
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 61
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.


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

All times are GMT -5. The time now is 05:14 AM.

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