命令簡介
watch
命令會以周期性的方式執(zhí)行給定的命令才睹,并全屏顯示執(zhí)行結果。
watch
命令是一個非常實用的 shell 命令甘邀,基本上所有的 Linux 發(fā)行版都自帶琅攘,watch
可以幫助檢測一個命令的運行結果,以解放我們的雙手松邪,避免一遍一遍地手動執(zhí)行檢測命令坞琴。
命令格式
watch
命令的幫助信息如下:
[root@A23488811 css]# 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).
常用的參數主要包括:
-n
或者 --interval
:watch
命令默認情況下每間隔 2 秒鐘執(zhí)行一次程序,可使用該參數指定間隔時間逗抑。
-d
或者 --differences
:高亮顯示變化的區(qū)域
-t
或者 --no-title
:會關閉 watch
命令在頂部顯示的時間間隔剧辐、命令寒亥、當前時間輸出等頭部信息
范例
每間隔 1 秒鐘高亮顯示網絡鏈接數的變化情況:
watch -n 1 -d netstat -ant
每間隔 1 秒鐘高亮顯示 http 鏈接數的變化情況:
watch -n 1 -d 'pstree|grep http'
監(jiān)控當前目錄中 scf 文件的變化情況:
watch -d 'ls -l | grep scf'
每間隔 5 秒鐘輸出系統(tǒng)的平均負載
watch -n 5 -d "uptime"