Skip to content

Instantly share code, notes, and snippets.

@Raboo
Last active August 1, 2025 07:47
Show Gist options
  • Save Raboo/e03054c9543e8eccec4eef070dc76c4e to your computer and use it in GitHub Desktop.
Save Raboo/e03054c9543e8eccec4eef070dc76c4e to your computer and use it in GitHub Desktop.
Top 5 CPU and Top 5 Memory users

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%

List top 5 users of CPU consumption and show their nproc, cpu, memory usage.

ps -eo user,pcpu,pmem | tail -n +2 | awk '{                                                                                                                                                                                                 num[$1]++; cpu[$1] += $2; mem[$1] += $3
}
END {
  printf("%-10s %-15s %-10s %-10s\n", "NPROC", "USER", "CPU", "MEM");

  # Store users in an array for sorting
  for (u in mem) {
    printf "%-10d %-15s %6.2f%%    %6.2f%%\n", num[u], u, cpu[u], mem[u] | "sort -k3 -nr"
  }
}' | head -n 6  # header + top 5

Example output:

NPROC      USER            CPU        MEM
53         user01          424.30%      6.60%
31         user02          199.00%      1.10%
30         user03          185.00%      1.60%
42         user04           94.00%      5.50%
31         user05           74.00%     13.70%

One-liner:

ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END {printf("%-10s %-15s %-10s %-10s\n", "NPROC", "USER", "CPU", "MEM"); for (u in mem) {printf "%-10d %-15s %6.2f%%    %6.2f%%\n", num[u], u, cpu[u], mem[u] | "sort -k3 -nr"}}' | head -n 6

List top 5 users of Memory consumption and show their nproc, cpu, memory usage.

ps -eo user,pcpu,pmem | tail -n +2 | awk '{                                                                                                                                                                                                 num[$1]++; cpu[$1] += $2; mem[$1] += $3
}
END {
  printf("%-10s %-15s %-10s %-10s\n", "NPROC", "USER", "CPU", "MEM");

  # Store users in an array for sorting
  for (u in mem) {
    printf "%-10d %-15s %6.2f%%    %6.2f%%\n", num[u], u, cpu[u], mem[u] | "sort -k4 -nr"
  }
}' | head -n 6  # header + top 5

Example output:

NPROC      USER            CPU        MEM
48         user01            6.60%     16.50%
31         user02           74.00%     13.70%
43         user03            8.90%     13.60%
53         user04          424.30%      6.60%
42         user05           94.10%      5.50%

One-liner:

ps -eo user,pcpu,pmem | tail -n +2 | awk '{num[$1]++; cpu[$1] += $2; mem[$1] += $3} END {printf("%-10s %-15s %-10s %-10s\n", "NPROC", "USER", "CPU", "MEM"); for (u in mem) {printf "%-10d %-15s %6.2f%%    %6.2f%%\n", num[u], u, cpu[u], mem[u] | "sort -k4 -nr"}}' | head -n 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment