The absolutely normal way to look at the logs is to write them to a file. When you want to see the logs, read the file. To watch lines as they get appended to the file, use a command such as tail -f
, which, after it reaches the end of the file, keeps it open and watches for extra lines that get appended to it. The option -f
is for “follow”, and in less you can have the same effect with F
for ”follow“; in other programs this may be known as “tailing” a file because tail
is the classic utility that does this. See the tail
tag on this site for more information about tailing utilities.
If you absolutely don't want to write the logs to a file, you can run the application in screen or tmux. To run /usr/bin/my_application --some-option
in the background, with screen:
screen -S myapp -d -m /usr/bin/my_application --some-option
To watch the logs:
screen -S myapp -d -r
Press Ctrl+A d to detach, i.e. to stop watching the logs. While you're watching the logs, you can also send input to the application, which may or may not desirable. To grant only read-only access, see Is there a way to run 'screen' in read-only mode?
With tmux, to start the application:
tmux new-session -s myapp -d /usr/bin/my_application --some-option
To view the latest logs:
tmux attach -r -t myapp
Press Ctrl+A d to detach. Run tmux attach
without -r
to be able to interact with the application.
A named pipe is not what you want. I'm just mentioning it because it superficially looks appropriate (and as I write it's popping up in comments). It will do what you describe:
mkfifo foo
while :; do echo 'abcdefg' >> foo; sleep 1; done
in parallel with
cat foo
This is not what you want because there has to be exactly one reader. If there's no reader, the writing side will block. If there are multiple readers, each line (or chunk, depending on how the program produces output) will be seen by exactly one reader, chosen more or less randomly. If there's a reader and it goes away, the writing side will fail to write and will receive a SIGPIPE if it hasn't disabled them.
foo
in this architecture is probably kafka. – jordanm Apr 14 '20 at 18:14