为了方便自己查看centOS上的各用户cpu和内存的使用比例,写了shell脚本。 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, mem