Tail log file on multiple machines over ssh
What you see is effect of a standard stdout buffer in grep
provided by Glibc. The best solution is to disable it by using --line-buffered
(GNU grep, I'm not sure what other implementations might support it or something similar).
As for why this only happens in some cases:
ssh server "tail -f /var/log/server.log | grep test"
runs the whole command in the quotes on the server - thus grep
waits to fill its buffer.
ssh server tail -f /var/log/server.log | grep test
runs grep
on your local machine on the output tail
sent through the ssh channel.
The key part here is, that grep
adjusts its behaviour depending on whether its stdin
is a terminal or not. When you run ssh -t
, the remote command is running with a controlling terminal and thus the remote grep
behaves like your local one.
check this out: multitail
MultiTail allows you to monitor logfiles and command output in multiple windows in a terminal, colorize, filter and merge.
To tail logs in multiple servers use:
multitail -l 'ssh user@host1 "tail -f /path/to/log/file"' -l 'ssh user@host2 "tail -f /path/to/log/file"'