List top 5 CPU users and list top 5 Memory users.
ps -eo user,pcpu,pmem | tail -n +2 | \
awk '{
num[$1]++; cpu[$1] += $2; mem[$1] += $3
}
END {
# Dump into files for sorting separately
for (u in cpu)
printf "%-10d %-15s %10.2f %10.2f\n", num[u], u, cpu[u], mem[u] > "/tmp/cpu_mem_usage.tmp"
# Sort separately
system("sort -k3 -nr /tmp/cpu_mem_usage.tmp | head -n 5 > /tmp/top_cpu.tmp")
system("sort -k4 -nr /tmp/cpu_mem_usage.tmp | head -n 5 > /tmp/top_mem.tmp")
# Print header
printf("%-40s %-40s\n", "Top 5 CPU Users", "Top 5 MEM Users");
printf("%-10s %-15s %-10s %-10s %-10s %-15s %-10s %-10s\n",
"NPROC", "USER", "CPU", "MEM", "NPROC", "USER", "CPU", "MEM");
# Load both results and print side-by-side
while ((getline cpu_line < "/tmp/top_cpu.tmp") > 0 && (getline mem_line < "/tmp/top_mem.tmp") > 0) {
split(cpu_line, c, " "); split(mem_line, m, " ");
printf("%-10s %-15s %6.2f%% %6.2f%% %-10s %-15s %6.2f%% %6.2f%%\n",
c[1], c[2], c[3], c[4], m[1], m[2], m[3], m[4]);
}
close("/tmp/cpu_mem_usage.tmp"); close("/tmp/top_cpu.tmp"); close("/tmp/top_mem.tmp");
}'
Output:
Top 5 CPU Users Top 5 MEM Users
NPROC USER CPU MEM NPROC USER CPU MEM
53 user01 424.30% 6.60% 48 user06 6.60% 16.50%
31 user02 199.00% 1.10% 31 user05 74.00% 13.70%
30 user03 185.00% 1.60% 43 user07 8.90% 13.60%
42 user04 94.10% 5.50% 53 user01 424.30% 6.60%
31 user05 74.00% 13.70% 42 user04 94.10% 5.50%