Linux的watch命令 — 实时监测命令的运行结果
watch 是一个非常实用的命令,基本所有的 Linux 发行版都带有这个小工具,如同名字一样,watch 可以帮你监测一个命令的运行结果,省得你一遍遍的手动运行。
直接在 watch 后面接你想运行的命令,watch 就会帮你重复运行,并把每次的结果都更新在屏幕上。
默认 watch 会以 2s 的间隔重复运行命令,你也可以用 -n 参数指定时间间隔~
还有一个实用的参数是 -d,这样 watch 会帮你高亮显示变化的区域,这样更加一目了然了~
Ctrl+c 就可以退出~
你可以拿他来监测你想要的一切命令的结果变化,比如 tail 一个 log 文件,ls 监测某个文件的大小变化,看你的想象力了~
FreeBSD和Linux下watch命令的不同
在Linux下,watch是周期性的执行下个程序,并全屏显示执行结果。
-d, –differences[=cumulative] 高亮显示变动
-n, –interval=<seconds> 周期(秒)
如:watch -n 1 -d netstat -ant
而在FreeBSD下的watch命令是查看其它用户的正在运行的操作,watch允许你偷看其它terminal正在做什么,该命令只能让超级用户使用。
如何运行watch:
root ttyp0 Oct 2 21:48 (192.168.x.x)
root ttyp1 Oct 2 22:25 (192.168.x.x)
xxhui ttyp3 Oct 2 23:48 (192.168.x.x)
[root@pdc conf]# watch ttyp3
有趣的linux命令–watch
其实这个命令应该是起源于FreeBSD中,但在其它的发行版中还是有这个命令的。
功能:实时监控某个命令,文件等“东西”的变化,并全屏显示
其实这个命令实现的结果和“tail -f”实现的差不多的,动态显示变化
[root@localhost ~]# man watch WATCH(1) Linux User’s Manual WATCH(1) NAME watch - execute a program periodically, showing output fullscreen SYNOPSIS watch [-dhvt] [-n <seconds>] [--differences[=cumulative]] [--help] [--interval=<seconds>] [--no-title] [--version] <command> DESCRIPTION watch runs command repeatedly, displaying its output (the first screen- full). This allows you to watch the program output change over time. By default, the program is run every 2 seconds; use -n or --interval to specify a different interval. The -d or --differences flag will highlight the differences between successive updates. The --cumulative option makes highlighting "sticky", presenting a running display of all positions that have ever changed. The -t or --no-title option turns off the header showing the interval, command, and current time at the top of the display, as well as the following blank line. watch will run until interrupted. NOTE Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect. Note that POSIX option processing is used (i.e., option processing stops at the first non-option argument). This means that flags after command don’t get interpreted by watch itself. EXAMPLES To watch for mail, you might do watch -n 60 from To watch the contents of a directory change, you could use watch -d ls -l If you’re only interested in files owned by user joe, you might use watch -d ’ls -l | fgrep joe’ To see the effects of quoting, try these out watch echo $$ watch echo ’$$’ watch echo "’"’$$’"’" You can watch for your administrator to install the latest kernel with watch uname -r (Just kidding.) BUGS Upon terminal resize, the screen will not be correctly repainted until the next scheduled update. All --differences highlighting is lost on that update as well. Non-printing characters are stripped from program output. Use "cat -v" as part of the command pipeline if you want to see them. AUTHORS The original watch was written by Tony Rems <rembo@unisoft.com> in 1991, with mods and corrections by Francois Pinard. It was reworked and new features added by Mike Coleman <mkc@acm.org> in 1999.
下面我们做个实验
Linux系统里有一些日志文件。观察这些日志文件是系统管理员的一个重要任务。你可以很方便地使用tail命令观察它们。但是如果你想要长时间监 视这些文件,每几分钟使用tail检查那些日志文件是一件很乏味的事情。你可以写一个短小的无限循环的脚本来周期性地检查文件,但其实已经有一个程序可以 为你处理这种重复的任务。
Linux watch 命令
Linux中的watch 命令提供了一种方式处理重复的任务。默认watch会每2秒重复执行命令。你一定也想到了,watch是一个很好的观察log文件的工具。下面是一个例子。
watch tail /var/log/syslog
想要停止命令的执行,只要使用标准的kill流程, [Ctrl]+C。
使用Linux watch命令监测syslog
你可以使用-n开关改变并指定时间间隔。要想每10秒检测日志文件,试试这个。
watch -n 10 tail /var/log/syslog
带有管道的watch命令
watch并不仅限于浏览日志文件。它可以用来重复你给它的任何命令。如果你要监测CPU的温度,你可以使用watch后跟上sensord命令来查看。
watch -n 1 sensors
我电脑上的输出看上去就像这样:
acpitz-virtual-0 Adapter: Virtual device temp1: +45.0°C (crit = +100.0°C)
我想过滤一下这个输出来只显示温度而不显示其他的。
我可以使用这个命令来查看
sensors | grep temp | awk '{ print $2 }'
记住,watch命令会重复它后面的第一个命令。必须要注意命令后面跟上管道的情况。你可以将你的命令放在引号里面来管理。
watch -n1 "sensors | grep temp | awk '{ print $2 }'"
带管道的watch命令
将watch作为时钟
就像你现在已经注意到的,watch执行后会在你的终端的右上角显示时间。我们可以通过传给watch一个空的命令参数来把它作为一个简单的时钟。 我们可以将一个空格包含在引号中来作为一个空的参数。
watch -n 1 " "
如你所见,这给予这个命令另外一个意义,手表(watch)。你可以把它作为你的腕表。
现在你知道如何使用Linux的watch命令。你要用它处理什么重复任务?
推荐阅读:
Linux流量监控工具 – iftop http://www.linuxidc.com/Linux/2013-08/89102.htm
Linux top 命令详解 http://www.linuxidc.com/Linux/2013-06/85626.htm
Linux下top命令 http://www.linuxidc.com/Linux/2013-04/83151.htm
Linux下高效的使用 top 命令 http://www.linuxidc.com/Linux/2013-04/82676.htm
Linux系统top命令详解 http://www.linuxidc.com/Linux/2012-12/76750.htm
Linux 系统监控负载top命令详解 http://www.linuxidc.com/Linux/2012-10/72756.htm






浙公网安备 33010602011771号