Stephen
Stephen

Reputation: 149

Best method to output log content to listening port

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

Answers (1)

SergA
SergA

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) variant

LOG_FILE="/var/log/custom_output.log"
PORT=5020
tail -n0 -F "$LOG_FILE" | nc -k -l -p $PORT

Notes:

  • Flag -k in nc is analog to --keep-open in "the OpenBSD rewrite of netcat";
  • Multiple clients can connect to 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 variant

LOG_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:

  • Each connected client will receive appended log lines at the same time;
  • Clients will not receive any previously buffered by tail strings.

additional

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

Related Questions