I know the top command to see the process of CPU and memory usage, but some users of the system can generate a lot of processes, if I wanna know total CPU and memory usage of an user,I must count it by my own,so,is there a command which can view total CPU and memory usage of per system user in Linux,and order by system username?
Asked
Active
Viewed 1.9k times
2 Answers
9
I don't think there's a direct way of doing it - but one way would be to parse the output of top. The following
top -b -n 1 -u username | awk 'NR>7 { sum += $9; } END { print sum; }'
does just that. For each process in top (for a given user) awk will strip the 9th delimited field (i.e. CPU %) 7 lines down (i.e. start of the top table) for each line, then sum them. Saves you fiddling about at least!
A couple of discussions around this...
4
Complementing @hygris command, we can add the following and have the information for all the users at the same time:
top -b -n 1 |\
awk ' BEGIN{OFS="\t"}
NR>7{ sum[$2] += $9; t += $9;}
END {print "user","%CPU" ; for (u in sum){print u,sum[u]}print "total",t}'
The above command will print the total at the end.
If you want to add the memory:
top -b -n 1 |\
awk ' BEGIN{OFS="\t"}
NR>7 { sum[$2] += $9; t += $9; mem[$2] += $10; tm += $10}
END {print "user","%CPU","%MEM" ; for (u in sum){
print u"\t"sum[u]"\t"mem[u];} print "total",t,tm}'
Then, of course, you can pipe and sort the output if that is your goal.
ChanganAuto
- 1
- 4
- 18
- 19
jcoder8
- 51