linux--watch命令使用 1.watch命令簡介 www.2cto.com watch [options] commandThe watch program executes a given command at regular inter-vals; the default is every two seconds. The command is passed to the shell (so be sure to quote or escape any special characters), and the results are displayed in a full-screen mode, so you can observe the output conveniently and see what has changed. For example, 每隔一段時間重複運行一個命令,預設間隔時間是2秒。要啟動並執行命令直接傳給shell(注意引用和轉義特殊字元)。結果會展示為全螢幕模式,這樣你可以很方便的觀察改變。例如: watch -n 60 date executes the date command once a minute, sort of a poor man’s clock. Type ^C to exit.執行date命令每分鐘一次,輸入^C 退出。 www.2cto.com 2.Useful options常用選項 -n seconds Set the time between executions, in seconds. 設定已耗用時間間隔,以秒為單位-d Highlight differences in the output, to emphasize what has changed from one execution to the next. 高亮輸出不同,強調兩次執行的不同。 3.watch 手冊NAME watch - execute a program periodically, showing output fullscreen watch - 定期執行程式,輸出全屏 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. 監視重複運行命令,展示輸出(全屏)。讓你監視程式輸出變化。預設程式2s運行一次;使用-n或--interval 指定不同的間隔時間 The -d or --differences flag will highlight the differences between successive updates. The --differences[=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. -d 或 --differences 會高亮顯示新內容。--cumulative 選項讓高亮保持,展示運行中所有的修改的內容。 -t 或 --no-title 選項關閉頭部展示,只展示空白行。 watch will run until interrupted. watch會一直運行,知道中斷。 NOTE Note that command is given to "sh -c" which means that you may need to use extra quoting to get the desired effect. 注意,命令"sh -c" 你需要應用起來才能達到預期的效果。 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. 注意,使用POSIX選項(選項會停在第一個非選項參數)意味著你不能在命令後再設定間隔。 www.2cto.com 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 想找joe使用者的檔案,是下面的命令 watch -d ’ls -l | fgrep joe’ To see the effects of quoting, try these out 看引號的使用 watch echo $$ watch echo ’$$’ watch echo "’"’$$’"’"結束