watch命令简介
watch命令可以重复的执行命令,并且将其输出在命令行上,可以方便的实现一些简单的监控
watch -n 1 cat 1.txt
上面的这个命令输出1.txt的内容,-n 1
表示每隔一秒钟执行一次命令
右侧shell执行命令,左侧是watch的输出
我们可以看到先是输出了wdnmd,然后输出了nmsl,至此,我们很简单的实现了一个不断变化的文件的监控,但它是实用的——比如监控一个动态变化的log日志
另一个小例子:在所有进程中查找是否有node关键字的进程,每隔三秒刷新一次
watch -n 3 "ps aux | grep node"
example
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 "'"'$$'"'"
To see the effect of precision time keeping, try adding -p to
watch -n 10 sleep 1
You can watch for your administrator to install the latest kernel with
watch uname -r
(Note that -p isn't guaranteed to work across reboots, especially in the face of ntpdate or other bootup time-changing mechanisms)
option
OPTIONS
-d, --differences [permanent]
Highlight the differences between successive updates. Option will read optional argument that changes highlight to be permanent, allowing to
see what has changed at least once since first iteration.
-n, --interval seconds
Specify update interval. The command will not allow quicker than 0.1 second interval, in which the smaller values are converted. Both '.' and
',' work for any locales.
-p, --precise
Make watch attempt to run command every interval seconds. Try it with ntptime and notice how the fractional seconds stays (nearly) the same, as
opposed to normal mode where they continuously increase.
-t, --no-title
Turn off the header showing the interval, command, and current time at the top of the display, as well as the following blank line.
-b, --beep
Beep if command has a non-zero exit.
-e, --errexit
Freeze updates on command error, and exit after a key press.
-g, --chgexit
Exit when the output of command changes.
-c, --color
Interpret ANSI color and style sequences.
-x, --exec
Pass command to exec(2) instead of sh -c which reduces the need to use extra quoting to get the desired effect.
-h, --help
Display help text and exit.
-v, --version
Display version information and exit.