監控使用CPU、記憶體前10的行程
寫法
- 使用top指令列印到一個臨時檔案
- 觀察檔案,提取需要的資訊
- 提取出第8行以后的資訊: tail -n +8 filename
- 配合管道,把第8行的資訊中的倆列放入一個陣列中
- awk ‘{array[$NF]+=$6}END{for (i in array) print array[i],i}’
- 然后 使用管道把以上資訊排序
- 排序之后 使用head -10
shell
#!/bin/bash
# 統計系統中前10使用記憶體最多的行程
memory() {
#收集任務管理器資訊
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#按照行程統計記憶體使用大小
tail -n +8 $temp_file | awk '{array[$NF]+=$6}END{for (i in array) print array[i],i}' |sort -k 1 -n -r|head -10
rm -f $temp_file
}
#統計系統中前10使用CPU的行程
cpu() {
#收集任務管理器資訊
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#按照行程CPU使用大小
tail -n +8 $temp_file | awk '{array[$NF]+=$9}END{for (i in array) print array[i],i}' |sort -k 1 -n -r|head -10
rm -f $temp_file
}
echo "記憶體前10"
memory
echo "CPU前10"
cpu
效果

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/260687.html
標籤:其他
上一篇:Linux(centos 7.5)服務器安裝Gitlab
下一篇:Linux 入門教程 1
