首页 > > > shell实战-监控使用CPU或内存前十名进程
监控使用CPU或内存前十名进程
监控目的
监控方法
监控实现
一、监控目的
掌握系统进程对系统资源的使用情况,掌握机器的动态。
二、监控方法
2.1)监控命令
ps
top
2.2)监控方法
通过对任务管理器中的进程对内存或CPU的使用情况进行整合、排序得出结论
三、监控实现
#!/bin/bash
#Description:
#Author: Bai Shuming
#Created Time: 2019/05/28 03:52
#统计使用内存和CPU前十名进程
#统计系统中前十名使用内存最多的进程
memory() {
#1、收集任务管理器进程信息
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#2、按进程统计内存使用大小
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
}
#统计系统中前十名使用CPU最多的进程
cpu() {
#1、收集任务管理器进程信息
temp_file=`mktemp memory.XXX`
top -b -n 1 > $temp_file
#2、按进程统计内存使用大小
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 memory
memory
echo cpu
cpu
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3122943/ ,如需转载,请注明出处,否则将追究法律责任。