Stream a continuously growing file over tcp/ip
Asked Answered
H

3

9

I have a project I'm working on, where a piece of Hardware is producing output that is continuously being written into a textfile. What I need to do is to stream that file as it's being written over a simple tcp/ip connection.

I'm currently trying to that through simple netcat, but netcat only sends the part of the file that is written at the time of execution. It doesn't continue to send the rest.

Right now I have a server listening to netcat on port 9000 (simply for test-purposes):

netcat -l 9000

And the send command is:

netcat localhost 9000 < c:\OUTPUTFILE

So in my understanding netcat should actually be streaming the file, but it simply stops once everything that existed at the beginning of the execution has been sent. It doesn't kill the connection, but simply stops sending new data.

How do I get it to stream the data continuously?

Hindquarter answered 3/6, 2010 at 16:2 Comment(1)
Named Pipe? en.wikipedia.org/wiki/Named_pipeWatchmaker
S
13

Try:

tail -F /path/to/file | netcat localhost 9000
Sot answered 3/6, 2010 at 16:9 Comment(4)
Yes, that sort of did it. I found tail in the win 2003 resource kit. Works like a charm. I changed the command to tail -f c:\out | netcat localhost 9000 Thanks!Hindquarter
once a wise guy said, use -F instead of -f, as it will add the extra option to retry in case the service was restarted or new log was generated!Longtin
This can't possibly work unless modified as described in @Grinner's comment (and should not have been marked accepted without that modification). < specifies that input should be read from a file named tail, not a command named tail.Jordaens
Yall feel free to make an edit and adjust it as needed.Sot
T
1

try:

tail /var/log/mail.log -f | nc -C xxx.xxx.xxx.xxx 9000
Tevis answered 17/10, 2018 at 5:6 Comment(2)
For bonus points it would be good to include some explanation of the code and why this solves the OP's problem.Redness
The -f should be before the filename to work with versions of tail that strictly comply with POSIX utility syntax guidelines entry #9, which specifies that all options should precede operands.Jordaens
H
-1

try nc:

# tail for get last text from file, then find the lines that has TEXT and then stream
# see documentation for nc, -l means create server, -k means not close when client disconnect, waits for anothers clients
tail -f  /output.log | grep "TEXT" | nc -l -k 2000
Harkness answered 23/9, 2020 at 20:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.