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> 周期(秒)
而在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
watch命令详解
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 screenfull). 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 youre only interested in files owned by user joe, you might use: watch -d ‘ls -l | fgrep joe’
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.
No Responses (yet)
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.