系统中的log文件会随着程序的运行而不断更新,使用使用cat命令不断查看,显然费时费力,本次研究自动更新到屏幕上,达到使用printf打印信息一样的效果。
- root@ubuntu:/home/csdn# watch --help
-
- Usage:
- watch [options] command
-
- Options:
- -b, --beep beep if command has a non-zero exit
- -c, --color interpret ANSI color and style sequences
- -d, --differences[=<permanent>]
- highlight changes between updates
- -e, --errexit exit if command has a non-zero exit
- -g, --chgexit exit when output from command changes
- -n, --interval <secs> seconds to wait between updates
- -p, --precise attempt run command in precise intervals
- -t, --no-title turn off header
- -x, --exec pass command to exec instead of "sh -c"
-
- -h, --help display this help and exit
- -v, --version output version information and exit
-
- For more details see watch(1).
- root@ubuntu:/home/csdn#
参数-n:表示每次执行的时间间隔
touch log
开启两个终端,一个终端监视log文件,另一个终端负责向log中写入信息
终端1执行:tail命令获取文件的最后10行信息
watch -n 1 tail log
终端2执行:
i=100;while [[ $i -lt 10000 ]]; do let "i+=1";echo hello_${i} >> log;sleep 1; done
执行结果如下:

只监视10行有点太少了,tail可以加参数-n。例如监视20行tail -n 20 log
watch -n 1 tail -n 20 log
执行后,如下所示:

试试弄个小视频
1667387538093
执行如下命令
tail -f log
可以看到屏幕每隔1秒回显示一行新的信息
