天天看點

watch 指令

 watch

可以看到所有資料包的大小: 

$ watch -d -n 1 ifconfig eth0

可以看到發送和接收了多少個package: 

$ watch netstat -s -u

watch 是一個非常實用的指令,基本所有的 Linux 發行版都帶有這個小工具,如同名字一樣,watch 可以幫你監測一個指令的運作結果,省得你一遍遍的手動運作。

直接在 watch 後面接你想運作的指令,watch 就會幫你重複運作,并把每次的結果都更新在螢幕上。

預設 watch 會以 2s 的間隔重複運作指令,你也可以用 -n 參數指定時間間隔~

還有一個實用的參數是 -d,這樣 watch 會幫你高亮顯示變化的區域,這樣更加一目了然了~

Ctrl+c 就可以退出~

你可以拿他來監測你想要的一切指令的結果變化,比如 tail 一個 log 檔案,ls 監測某個檔案的大小變化,看你的想象力了~

<b>FreeBSD和Linux下watch指令的不同 </b>

在Linux下,watch是周期性的執行下個程式,并全屏顯示執行結果。 

-d, --differences[=cumulative]       高亮顯示變動

-n, --interval=              周期(秒)

如:watch -n 1 -d netstat -ant

而在FreeBSD下的watch指令是檢視其它使用者的正在運作的操作,watch允許你偷看其它terminal正在做什麼,該指令隻能讓超級使用者使用。

如何運作watch:

[root@pdc conf]# who

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

~~~~~~~~~~~~~~~~~~~~~~~~~~~

tail本身的功能是顯示檔案的後多少行

tail filename:顯示filename後十行

tail -n filename 顯示filename後n行

通過添加-f選項可以監控檔案變化,檔案有更新就會列印出來:tail -f filename

而且watch的原理就是重複的執行後面的指令,預設的時間間隔是2秒.

如watch -d -n 10 cat /etc/syslog.conf

每10秒列印一下/etc/syslog.conf檔案,-d表示高亮變化的部分

這兩個指令在監控日志檔案的時候相當有用

有點注意的是經測試不能在背景運作

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

<b>watch指令詳解</b>

<b>NAME</b>

       watch - execute a program periodically, showing output fullscreen 

<b>SYNOPSIS</b>

       watch  [-dhvt]  [-n ] [--differences[=cumulative]] [--help] [--interval=] [--no-title] [--version] 

<b>DESCRIPTION</b>

       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.

<b>NOTE</b>

       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.

<b>EXAMPLES</b>

       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 &amp;#124; fgrep joe'

       You can watch for your administrator to install the latest kernel with:  watch uname -r  (Just kidding.)呵呵

<b>BUGS</b>

       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.

 其實很簡單,就是利用linux下的 watch 工具來做監控,方法如下:

[yejr@localhost imysql]# watch -d -n 10 "egrep ’MySQL thread|Log|Modified db pages’ innodb_status.3249 "

Every 10.0s: egrep ’MySQL thread|Log|Modified db pages’ innodb_status.3249

Thu Apr 9 10:01:12 2009

MySQL thread id 6, query id 71 localhost root Sending data

Log sequence number 2703 3443241402

Log flushed up to  2703 3442763607

Modified db pages    83325

或者:

[yejr@localhost imysql]#watch -d -n 10 "mysqladmin ext|egrep Innodb_data"

Every 10.0s: mysqladmin ext|egrep Innodb_data

Thu Apr 9 10:03:55 2009

| Innodb_data_fsyncs        | 4144699   |

| Innodb_data_pending_fsyncs    | 0      |

| Innodb_data_pending_reads     | 0      |

| Innodb_data_pending_writes    | 0      |

| Innodb_data_read         | 5567172608 |

| Innodb_data_reads         | 298413   |

| Innodb_data_writes        | 4492881   |

| Innodb_data_written        | 18549422080 |

參數解釋:

-d --differences

-n --interval

不用我多說了吧,怎麼樣,是不是覺得很有意思

繼續閱讀