Linux Bash 脚本日志文件连续显示到屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6143838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script log file display to screen continuously
提问by Aaron Murray
I am creating an application that writes to a log file, and I need to know how in Linux / Bash to continuously display the log file to the screen (updating the screen with every new line put into the log).
我正在创建一个写入日志文件的应用程序,我需要知道如何在 Linux/Bash 中将日志文件连续显示到屏幕上(用放入日志的每一行更新屏幕)。
So as an example, lets say I want to push a running log of apache/error.log
to the screen (ssh terminal) continuously updating.
举个例子,假设我想将一个运行日志推apache/error.log
送到屏幕(ssh 终端)不断更新。
采纳答案by Hai Vu
Try the tail command:
试试tail命令:
tail -f filename
回答by bukzor
ssh {remotehost} tail -n0f {logfile}
ssh {remotehost} tail -n0f {logfile}
This will give you zero lines initially, and continuously print any new lines that appear in the file.
这最初会给你零行,并不断打印文件中出现的任何新行。
回答by osgx
Another solution is
另一个解决方案是
less +F filename
or just less filename
and typing "F" into it (pressing shift+f). It can be better than tail
, because it allows you to cancel continuous printing temporary, go backward to look something and reenable it with "F" (shift+f) again
或者只是在less filename
其中输入“F”(按shift+ f)。它可能比 更好tail
,因为它允许您暂时取消连续打印,返回查看某些内容并再次使用“F”(shift+ f)重新启用它
回答by jm666
You can also:
你也可以:
less filename.txt
and press 'F'
has one plus - you can anytime CTRL-C and scroll back in log and start watching again with the 'F'.
有一个加号 - 您可以随时按 CTRL-C 并在日志中向后滚动,然后使用“F”再次开始观看。
回答by Seth Robertson
The watch command can also be of use.
watch 命令也很有用。
watch tail logfile
Would show you the last 5 lines of the log file. It can be extended to any command which prints stuff to stdout.
将显示日志文件的最后 5 行。它可以扩展到任何将内容打印到标准输出的命令。
Yes, using tail -f
is the traditional solution, but depending on what you are trying to do, this might work better.
是的,使用tail -f
是传统的解决方案,但根据您尝试执行的操作,这可能会更有效。