Any way to stream a file in linux

I have a terminal output which I'd like to be available to several users

currently something like:

command > /var/logs

for the input, and:

watch tail -30 /var/logs

for the input terminals.

however this is undesirable because watch only polls as opposed to outputting when there is updates and tail doesn't allow scrollback.


Solution 1:

Two problems you posit:

watch only polls as opposed to outputting when there are updates

tail -f does not poll. As of version 7.5 of GNU Coreutils, tail -f will use the Linux kernel's inotify interface to receive signals from the kernel indicating that the file has been modified. If you don't use GNU Coreutils, or use a version older than 7.5 for some reason, it will poll once per second. It probably also polls on non-Linux POSIX operating systems (BSD, Solaris, etc) but you would have to research it on a case-by-case basis: some of these OSes use GNU Coreutils but the kernel doesn't support inotify; some of them don't use GNU Coreutils to implement tail at all.

tail doesn't allow scrollback

The less command is specifically designed to buffer data from stdin (e.g. from the tail command) and allow you to scroll back and forth. The buffer is not unlimited, but you can use command line options to increase or decrease the buffer as well as modify how long the buffer is for back-scroll and forward-scroll.

So something like, maybe,

tail -f /var/logs | less

would allow you to keep a scrollback buffer of the logs without polling (tail just sits there idle consuming no CPU until the kernel tosses it a signal letting it know that the file has been modified).

If you need multiple users to be able to read from the same buffer, you could just run tail -f /var/logs within a screen session. My original answer didn't take into account that you want to run this so that multiple users can see the same log data at the same time. @Let_Me_Be mentioned screen and then it occurred to me that that's probably your best bet.

So putting my solution and @Let_Me_Be's solution together:

screen -mdS logview -- tail -f /var/logs

Warning: Untested. Please edit if it's wrong. But I think we're close on the trail to a proper solution here.

Now you just need to have screen in multi-user mode. This is a little complicated, so I'll let this site speak for itself. The gist is that you have to add multiuser on to your ~/.screenrc file. Also see the Common Problems at the bottom of that link.

Now, for users to view the log:

screen -rS logview

should do it.

Solution 2:

You want a program like screen or tmux.