linux shell腳本攻略 第八章 當個好管家 監控磁盤 du,time,iotop,inotifywait 等

1.查看磁盤使用

du f1
-a:遞歸輸出當前所有文件
-h:以k,m顯示當前的文件大小
-k:以kb爲大小顯示
-c:統計文件列表總共佔用大小
-s:只顯示總計大小
–exclude-form:排除文件列表
–max-depth:最大深度

Ian>du -ah ./
4.0K	./a.txt
4.0K	./b.txt
4.0K	./a.txt.bakfile
4.0K	./cut/a.txt
8.0K	./cut
24K	./
Ian>du -h ./
8.0K	./cut
24K	./
Ian>du -sh ./
24K	./
Ian>du -ak ./ | sort -nrk 1|head -n 3#查找前三大小的文件和目錄
287952	./
166028	./VMwareTools-10.3.10-13959562
166024	./VMwareTools-10.3.10-13959562/vmware-tools-distrib
Ian>find . -type f -exec du -k {} \; | sort -nrk 1 | head -n 3#查找前三大小的文件
4	./test.txt
4	./sed/cut/a.txt
4	./sed/b.txt

2.計算命令執行時間

time command:獲取運行時間
time -o file command:將運行時間輸出到文本
time -a -o file command:將時間追加到文本
time -f “time_format” command:將時間格式寫入文本,其中%e爲real時間,%U爲user時間,%S爲系統時間

  • -f的參數說明
    https://blog.csdn.net/qq_34595352/article/details/86702411
    • Real時間:命令從開始執行到結束執行的時間,包括其他進程所佔用的時間片及被進程阻塞所花費的時間,如等待io操作的時間等
    • User時間:花費在用戶模式下的時間,真正用於執行進程所用的時間,執行其他進程以及阻塞時間沒有計算
    • Sys時間:花費在內核中的CPU時間,代表在內核中執行系統調用所用時間
Ian>time ls
a.log  a.sh  awk  cut  grep  out.html  paste  sed  test.txt  tree1  tree2

real	0m0.001s
user	0m0.001s
sys	0m0.000s
Ian>/usr/bin/time -a -o time_find.log find / -iname "*.txt"
Ian>cat time_find.log
Command exited with non-zero status 1
0.13user 1.16system 0:02.05elapsed 63%CPU (0avgtext+0avgdata 5016maxresident)k
23590inputs+0outputs (0major+600minor)pagefaults 0swaps
Ian>/usr/bin/time -f "Real Time: %r" ls
a.log  a.sh  awk  cut  grep  o.log  out.html  paste  sed  test.txt  time_find.log  time.log  tree1  tree2
Real Time: 0

3.收集登錄用戶信息

who:獲取當前用戶
w:獲取登錄用戶更詳細信息
users:當前登錄的所有用戶
uptime:查看系統已運行時間
last:獲取上次啓動及登錄會話的信息

Ian>who
ian      :0           2020-04-09 21:15 (:0)
Ian>w
 11:44:57 up 7 days, 4 min,  1 user,  load average: 0.00, 0.01, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
ian      :0       :0               Thu21   ?xdm?  12:24   0.01s /usr/lib/gdm3/gdm-x-session --run-script env GNOME_SHELL_SESSION_MODE=ubuntu gnome-session --session=ubuntu
Ian>users
ian
Ian>uptime
 11:45:06 up 7 days, 4 min,  1 user,  load average: 0.00, 0.01, 0.00
Ian>last
ian      :0           :0               Thu Apr  9 21:15   still logged in
ian      :0           :0               Wed Apr  8 11:41 - 21:15 (1+09:33)
reboot   system boot  5.3.0-46-generic Wed Apr  8 11:40   still running
ian      :0           :0               Wed Apr  8 11:33 - 11:40  (00:07)
reboot   system boot  5.3.0-46-generic Wed Apr  8 11:32 - 11:40  (00:07)
ian      :0           :0               Wed Apr  8 10:33 - down   (00:57)

wtmp begins Wed Apr  8 10:33:26 2020

4.監控命令輸出(watch)

watch ls:監控ls輸出,默認2s一次
watch -n 5 ls:5s一次
watch -d ls:不同以顏色顯示:

Ian>watch -n 3 ls
Every 3.0s: ls                 ian-virtual-machine: Wed Apr 15 11:52:27 2020

a
b

5.記錄文件及目錄訪問(inotifywait)

inotifywait -m -r -e create,move,delete ./ -q:監控目錄的創建,修改,刪除

Ian>inotifywait -m -r -e create,move,delete ./ -q
./ CREATE,ISDIR new
./new/ CREATE a.txt
./new/ CREATE a.txt.bakfile
./new/ CREATE b.txt
./new/ DELETE a.txt
./new/ DELETE b.txt
./new/ DELETE a.txt.bakfile
./ DELETE,ISDIR new

-m:持續監控,而不是事發之後退出
-r:遞歸監控
-e:監控的事件,可選事件如下:modify、delete、create、close_write、move、close、unmount和attrib等

  • -e可選事件
    http://blog.chinaunix.net/uid-31484238-id-5784955.html

6.使用syslog監控日誌(logger)

logger message:向/var/log/syslog中寫日誌
logger -t -t TAG logline:在日誌中加tag

Ian>logger this is a log line
Ian>tail -n 2 syslog
Apr 15 12:10:40 ian-virtual-machine ian: 
Apr 15 12:12:28 ian-virtual-machine ian: this is a log line
Ian>logger -t TESTTAG this is a log line
Ian>tail -n 2 syslog
Apr 15 12:12:28 ian-virtual-machine ian: this is a log line
Apr 15 12:15:23 ian-virtual-machine TESTTAG: this is a log line

7.監控io(iotop)

iotop -o:監視正在io的進程的情況,排除干擾
iotop -b -n 2:只監視兩條然後退出
iotop -p PID:監視某個進程

Ian>sudo iotop
Total DISK READ :       0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND                                                                                                                                    
     1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % init splash
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
Ian>sudo iotop -p 8862
Total DISK READ :       0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:       0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND                                                                                                                                    
  8862 be/4 ian         0.00 B/s    0.00 B/s  0.00 %  0.00 % Xorg vt2 -displayfd 3 -auth /run/user/1000/gdm/Xauthority -background none -noreset -keeptty -verbose 3

8.檢查磁盤文件系統錯誤(fsck)

fsck /dev/sda1:檢查文件系統
fsck -A:檢測/etc/fstab中所配置的文件系統
fsck -a /dev/sda1:修復文件系統

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章