编写shell脚本查看linux当前各用户的cpu和memory消耗比例
2011-01-15 12:07
851 查看
为了方便自己查看centOS上的各用户cpu和内存的使用比例,写了shell脚本。
viewUsage.sh
使用说明:
#以下输出均为“aix.unix-center.net”上的运行结果
1. 不使用参数,直接调用,默认排序为按user名升序,cpu或mem的使用率超过10%时以红色字体显示:
#提示:大写字母开头会排在小写字母开头前面
2. 使用--reverse参数,使用user名反向排序:
3. 使用--cpu,按cpu使用率排序(--reverse可以反向排序)
4.使用--mem,按memory(内存)使用率排序(--reverse可以反向排序)
viewUsage.sh
#!/bin/bash # # view the cpu and memory consumption of each user at the current time. # chenqy 20101126 v0.9 # chenqy 20110115 v1.0 : # added the sort option: --reverse --mem --cpu function viewconsumption { ps aux | grep -v 'PID' | sed 's/[ ][ ][ ]*/ /g' | cut -d " " -f1-4 | sort | awk ' BEGIN{ userid = "None" cpuUsage = 0 memUsage = 0 } { if(userid == $1) { cpuUsage += $3 memUsage += $4 } else { if (userid != "None") { printf("%s %4.1f %4.1f/n", userid, cpuUsage, memUsage) } userid = $1 cpuUsage = $3 memUsage = $4 } }' } function printResult { awk ' BEGIN { postcolor = "/033[0;39m" } { userid = $1 cpuUsage = $2 memUsage = $3 if(cpuUsage >= 10 || memUsage >= 10) { # red color precolor = "/033[0;31m" } printf("%s%s /033[12G%4.1f /033[20G%4.1f%s/n", precolor, userid, cpuUsage, memUsage, postcolor) precolor = "/033[0;39m" }' } echo -e "USER /033[12G%CPU /033[20G%MEM" echo "--------- ------ -----" if [ "$1" == "--mem" ];then key="-k 3 -n" elif [ "$1" == "--cpu" ];then key="-k 2 -n" fi if [ "$1" == "--reverse" -o "$2" == "--reverse" ];then reverse="-r" fi viewconsumption|sed 's/[ ][ ][ ]*/ /g'|sort $key $reverse|printResult
使用说明:
#以下输出均为“aix.unix-center.net”上的运行结果
1. 不使用参数,直接调用,默认排序为按user名升序,cpu或mem的使用率超过10%时以红色字体显示:
#提示:大写字母开头会排在小写字母开头前面
tsingyee@aix bin# ./viewUsage.sh USER %CPU %MEM --------- ------ ----- Michelle 48.2 0.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) bookses 0.0 0.0 centos1 0.0 0.0 coolryx 0.0 0.0 daemon 0.0 0.0 dddfr 0.0 0.0 erryqj 0.0 0.0 firstdre 0.0 0.0 freebsdj 0.0 0.0 freshman 0.0 0.0 geepz 0.0 0.0 hanshanz 0.0 0.0 he_pdz 0.0 0.0 huangxue 0.0 0.0 idlanhy 0.0 0.0 jerry168 0.0 0.0 junjieai 0.0 0.0 junonly 0.0 0.0 lichd 0.0 0.0 lqjboy 0.0 0.0 markcool 0.0 0.0 mxdu 0.0 0.0 nanman 0.0 0.0 philippe 0.0 0.0 piaoye06 0.0 0.0 root 14.1 2.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) spectre 0.0 0.0 thorqq 0.0 0.0 tsingyee 0.0 0.0 uingei 0.0 0.0 zhangdon 0.0 0.0 zhangwb8 0.0 0.0 zte_zxr1 0.0 0.0 tsingyee@aix bin#
2. 使用--reverse参数,使用user名反向排序:
tsingyee@aix bin# ./viewUsage.sh --reverse USER %CPU %MEM --------- ------ ----- zte_zxr1 0.0 0.0 zhqing21 0.0 0.0 zhangwb8 0.0 0.0 zhangdon 0.0 0.0 uingei 0.0 0.0 tsingyee 0.0 0.0 thorqq 0.0 0.0 spectre 0.0 0.0 root 14.1 2.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) piaoye06 0.0 0.0 philippe 0.0 0.0 nanman 0.0 0.0 mxdu 0.0 0.0 markcool 0.0 0.0 lqjboy 0.0 0.0 linta 0.0 0.0 lichd 0.0 0.0 junonly 0.0 0.0 junjieai 0.0 0.0 jerry168 0.0 0.0 idlanhy 0.0 0.0 huangxue 0.0 0.0 he_pdz 0.0 0.0 geepz 0.0 0.0 freshman 0.0 0.0 freebsdj 0.0 0.0 firstdre 0.0 0.0 erryqj 0.0 0.0 dddfr 0.0 0.0 daemon 0.0 0.0 coolryx 0.0 0.0 centos1 0.0 0.0 bookses 0.0 0.0 Michelle 48.2 0.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) tsingyee@aix bin#
3. 使用--cpu,按cpu使用率排序(--reverse可以反向排序)
tsingyee@aix bin# ./viewUsage.sh --cpu --reverse USER %CPU %MEM --------- ------ ----- Michelle 48.2 0.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) root 14.1 2.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) zte_zxr1 0.0 0.0 zhqing21 0.0 0.0 zhanzhiy 0.0 0.0 zhangwb8 0.0 0.0 zhangdon 0.0 0.0 uingei 0.0 0.0 tsingyee 0.0 0.0 thorqq 0.0 0.0 spectre 0.0 0.0 piaoye06 0.0 0.0 philippe 0.0 0.0 nanman 0.0 0.0 mxdu 0.0 0.0 markcool 0.0 0.0 lqjboy 0.0 0.0 linta 0.0 0.0 lichd 0.0 0.0 junonly 0.0 0.0 jerry168 0.0 0.0 idlanhy 0.0 0.0 huangxue 0.0 0.0 he_pdz 0.0 0.0 geepz 0.0 0.0 freshman 0.0 0.0 freebsdj 0.0 0.0 firstdre 0.0 0.0 erryqj 0.0 0.0 dddfr 0.0 0.0 daemon 0.0 0.0 coolryx 0.0 0.0 centos1 0.0 0.0 bookses 0.0 0.0 tsingyee@aix bin#
4.使用--mem,按memory(内存)使用率排序(--reverse可以反向排序)
tsingyee@aix bin# ./viewUsage.sh --mem --reverse USER %CPU %MEM --------- ------ ----- root 14.1 2.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) zte_zxr1 0.0 0.0 zhqing21 0.0 0.0 zhanzhiy 0.0 0.0 zhangwb8 0.0 0.0 zhangdon 0.0 0.0 uingei 0.0 0.0 tsingyee 0.0 0.0 thorqq 0.0 0.0 spectre 0.0 0.0 piaoye06 0.0 0.0 philippe 0.0 0.0 nanman 0.0 0.0 mxdu 0.0 0.0 markcool 0.0 0.0 lqjboy 0.0 0.0 linta 0.0 0.0 lichd 0.0 0.0 junonly 0.0 0.0 jerry168 0.0 0.0 idlanhy 0.0 0.0 huangxue 0.0 0.0 he_pdz 0.0 0.0 geepz 0.0 0.0 freshman 0.0 0.0 freebsdj 0.0 0.0 firstdre 0.0 0.0 erryqj 0.0 0.0 dddfr 0.0 0.0 daemon 0.0 0.0 coolryx 0.0 0.0 centos1 0.0 0.0 bookses 0.0 0.0 Michelle 48.2 0.0 <=这条记录为红色字体(cpu或mem的使用率超过10%) tsingyee@aix bin#
相关文章推荐
- 在linux中执行shell脚本,当前用户执行没有问题,设置为定时任务却报错
- Shell命令编写——查看当前登陆用户的登录时间ac,last
- linux系统的负载与CPU、内存、硬盘、用户数监控shell脚本
- 利用Shell脚本来监控Linux系统的负载、CPU、内存、硬盘、用户登录数
- shell脚本: linux系统的负载与CPU、内存、硬盘、用户数监控
- linux-raspbian系统下编写python脚本显示树莓派的当前cpu温度、使用率、内存和硬盘信息
- linux系统的负载与CPU、内存、硬盘、用户数监控shell脚本
- Linux下查看所有用户(shell脚本获取)
- Linux中使用Shell脚本查看Java线程的CPU使用情况
- Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
- Shell命令编写——查看当前用户who
- linux的shell脚本判断当前是否为root用户
- Java 连接远程Linux 服务器执行 shell 脚本查看 CPU、内存、硬盘信息
- shell脚本: 查看linux用户组,以及每个组中有哪些用户
- linux的shell脚本判断当前是否为root用户
- Linux中使用Shell脚本查看Java线程的CPU使用情况
- Linux下查看所有用户(shell脚本获取)
- 利用Shell脚本来监控Linux系统的负载、CPU、内存、硬盘、用户登录数
- Linux系统的负载与CPU、内存、硬盘、用户数监控的shell脚本
- Linux查看磁盘信息(编写shell脚本来分析文件的占用情况)