watch 命令不是一般的有用

先分享兩個對軟件開發非常不錯的工具

watch 可以監測一個命令的運行結果,來監測你想要的一切命令的結果變化

Usage: watch [-dhntv] [--differences[=cumulative]] [--help] [--interval=<n>] [--no-title] [--version] <command>
  -d, --differences[=cumulative]        highlight changes between updates
                (cumulative means highlighting is cumulative)
  -h, --help                            print a summary of the options
  -n, --interval=<seconds>              seconds to wait between updates
  -v, --version                         print the version number
  -t, --no-title                        turns off showing the header                    turns off showing the header

每隔一秒高亮顯示網絡鏈接數的變化情況

watch -n 1 -d netstat -ant 【-n 設置間隔,-d,difference,高亮顯示不同】
watch -d 'ls /home/omd'    【-d 高亮顯示】
watch -t 'ls /home/omd'    【-t會關閉watch命令在頂部的時間間隔】
說明: 切換終端:Ctrl+x     退出watch:Ctrl+g

每隔一秒高亮顯示 http 鏈接數的變化情況

watch -n 1 -d 'pstree|grep http'

實時查看模擬攻擊客戶機建立起來的連接數

watch -n 1 -d 'netstat -an | grep "21" | egrep "192.168.25.100"| wc -l'

監測當前目錄中 scf' 的文件的變化

watch -d 'ls -l|grep scf'

10 秒一次輸出系統的平均負載

watch -n 1 -d "uptime"

實際應用場景

在排查線上系統性能瓶頸時,監控 jvm 的堆棧就可以使用 watch 命令。

jps

如圖

雖然可以使用 jstack 可以監控 jvm 的線程堆棧信息,但是不能動態監控;這時 watch 命令就可以派上用場了

watch -n 1 "jstack 13671"

另外,還可以使用 grep 命令進行內容刷選

watch -n 1 "jstack 13671 | grep RUNNING"
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/_0V8yfiL6HfP0Pr4mXgixw