Linux – How to view total CPU and memory usage of per system user in Linux

cpucpu usagelinuxmemoryuser

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?

Best Answer

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...

Howtoforge, Stackexchange

Related Question