Reputation: 149
I am outputting content of a log via netcat to an application over the network. I don't know if what I'm doing is the most efficient, especially when I notice the netcat session becomes non-responsive. I have to stop netcat and start it again for the application to work again.
The command I run is:
/bin/tail -n1 -f /var/log/custom_output.log | /bin/nc -l -p 5020 --keep-open
This needs to run like this 24/7. Is this the most efficient way of doing it? How can I improve on it so I don't have to restart the process daily?
EDIT
So I realised that when the log is being rotated, netcat is locked onto a file that's not longer being written to. I can deal with this easily enough.
The question still stands. Is this the best way to do something like this?
Upvotes: 1
Views: 1756
Reputation: 1174
It's been 6 years, but maybe someone will come in handy.
To account for log rotation, use tail
with the -F
flag.
nc
(aka netcat
) variantLOG_FILE="/var/log/custom_output.log"
PORT=5020
tail -n0 -F "$LOG_FILE" | nc -k -l -p $PORT
Notes:
-k
in nc
is analog to --keep-open
in "the OpenBSD rewrite of netcat";nc
at the same time, but only the first one will be receive appended log lines;tail
will run immediately, so it will collect appended log lines even if no client is connected. Thus, the first client can receive some buffered data - all log lines that have been appended since tail
was run.socat
variantLOG_FILE="/var/log/custom_output.log"
PORT=5020
socat TCP-LISTEN:$PORT,fork,reuseaddr SYSTEM:"tail -n0 -F \"$LOG_FILE\" </dev/null"
Note: here socat
will fork (clone itself) on each client connection and start a separate tail
process. Thus:
tail
strings.You can redirect stderr
to stdout
in the tail
process by adding 2>&1
(in both variants). In this case, clients will receive auxiliary message lines, e.g.:
tail: /var/log/custom_output.log: file truncated
;tail: '/var/log/custom_output.log' has become inaccessible: No such file or directory
- printed when the log file has been removed or renamed, only if -F
is used;tail: '/var/log/custom_output.log' has appeared; following new file
- printed when a new log file is created, only if -F
is used.Upvotes: 1