watch 명령을 이용한 linux 시스템 모니터링
 
watch 명령은 원하는 명령어의 결과를 원하는 시간(초) 주기로 리프레시 하여 결과를 보여주는 명령어 입니다.
시스템의 자원 사용량을 모니터링 하거나, 특정 명령어를 반복적으로 입력해서 결과를 확인해야 할 때, 유용하게 사용할 수 있는 명령어 입니다. 
 
[root@localhost ~]# watch --help
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
[root@localhost ~]# 
 
 
기본 사용 방법)
[root@localhost]# watch [-d] [-n seconds] 'command'
종료는 [Ctrl + c]
 
-d 옵션은 이전 출력결과와 비교하여 변경된 부분을 표시해 주는 기능
-n 옵션은 입력한 명령어의 결과를 원하는 초단위 간격으로 출력해 주는 기능(default 값은 2초)
 
====================================================================
 
예제) 
 
- 시스템의 uptime 값을 화면에 계속 출력(2초간격)
 
  [root@localhost ~]# watch uptime
 
  Every 2.0s: uptime                                                                                       Thu Mar 29 01:49:12 2018
 
   01:49:12 up 83 days, 18:44,  1 user,  load average: 0.00, 0.00, 0.00
 
 
 
- command에 띄어쓰기가 들어갈 경우는 ' ' 으로 묶어 준다. uptime 값을 5초 간격으로 화면에 계속 출력
 
  [root@localhost ~]# watch -d -n 5 'cat /proc/uptime'
 
  Every 5.0s: cat /proc/uptime                                                                           Thu Mar 29 01:51:19 2018
 
  7238809.82 14469889.75
 
 
 
- 10초 간격으로 iostat 모니터링
 
  [root@localhost local]# watch -n 10 iostat
 
  Every 2.0s: iostat                                                                                          Thu Mar 29 01:56:39 2018
 
  Linux 2.6.32-696.el6.x86_64 (localhost.localdomain)     03/29/2018      _x86_64_        (2 CPU)
 
  avg-cpu:  %user   %nice %system %iowait  %steal   %idle
             0.01    0.00    0.01    0.02    0.03   99.93
 
  Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
  xvda              0.20         0.48         2.80    3503370   20247044
 
 
- 5초 간격으로 vmstat 모니터링
 
  [root@localhost local]# watch -n 5 vmstat
 
  Every 2.0s: vmstat                                                                                          Thu Mar 29 01:57:11 2018
 
  procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu-----
   r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
   0  0   9636 1224232 148464 483280    0    0     0     1    3    3  0  0 100  0  0
 
 
- 1초 간격으로 apache 프로세스 모니터링
 
  [root@localhost local]# watch -d -n 1 'ps -ef | grep httpd | grep -v grep'
 
  Every 1.0s: ps -ef | grep httpd | grep -v grep                                                        Thu Mar 29 01:55:20 2018
 
  root     10214     1  0 01:54 ?        00:00:00 /usr/local/apache/bin/httpd -k start
  daemon   10217 10214  0 01:54 ?        00:00:00 /usr/local/apache/bin/httpd -k start
  daemon   10218 10214  0 01:54 ?        00:00:00 /usr/local/apache/bin/httpd -k start
  daemon   10219 10214  0 01:54 ?        00:00:00 /usr/local/apache/bin/httpd -k start
 
 
- 여러가지 명령어를 조합해서도 사용 가능(2초 간격)
   w 명령어를 통한 현재 접속자, loadaverage, uptime 확인
   df 명령을 통한 디스크 사용량
   free 명령을 통한 memory 사용량 
 
[root@localhost local]# watch -d 'w; echo; df -h; echo; free -m'
 
  Every 2.0s: w; echo; df -h; echo; free -m                                                             Thu Mar 29 02:02:44 2018
 
   02:02:44 up 83 days, 18:58,  1 user,  load average: 0.00, 0.00, 0.00
  USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
  root     pts/0    10.10.0.1        01:38   12.00s  0.20s  0.02s watch -d w; ech
 
  Filesystem      Size  Used Avail Use% Mounted on
  /dev/xvda3      7.6G  6.0G  1.2G  84% /
  tmpfs           936M     0  936M   0% /dev/shm
  /dev/xvda1      240M   35M  193M  16% /boot
 
               total       used       free     shared    buffers     cached
  Mem:          2000        805       1194          0        144        471
  -/+ buffers/cache:        188       1811
  Swap:         2047          9       2038