4

I want to use ps on my desktop through geektools to see what processes are using what. Currently my command is:

ps -amcwwwxo "command %mem %cpu" | grep -v grep | head -13

The problem with this, is seeing as I'm using chrome, the process "Google Chrome He" takes up most of the 13 display lines.

Is there any way to sum together the mem and cpu usage of all processes of the same name? Either via ps or piping it through another command.

skeletalmonkey
  • 726
  • 1
  • 10
  • 20

4 Answers4

3

Looking for the same, I figured out this

ps aux | awk '{arr[$1]+=$4}; END {for (i in arr) {print i,arr[i]}}' | sort -k2

to print processes ordered by mem, grouped by user (column1, the $1), you can group by other things, and sum other things, changing $1 and $4

  • $1 is the first column: user name (groups by this)
  • $4 is the fourth column: %mem (sums this)

I was happy to find the solution, just wanted to share.

jperelli
  • 6,988
  • 5
  • 50
  • 85
3

You may use a combination of awk and sort:

(
printf "%-20s %-8s %-8s\n" "COMMAND" "%MEM" "%CPU"
/bin/ps -amcwwwxo "command %mem %cpu" | 
/usr/bin/awk -F" " '
BEGIN { 
  idx=0 
  format="%-20s /%-8s/ %-8s\n"
}
{
  idx = idx + 1
  col1=$0
  col2=$(NF-1)
  col3=$NF
  sub(/[[:space:]]+[^ ]+[[:space:]]+[^ ]+[[:space:]]*$/,"", col1)
  a[idx]=col1
  b[col1]+=col2
  c[col1]+=col3
}
END {
  for(i=2; i<=idx; i++) 
  {
    if (a[i] in b)
    {
      printf format, a[i], b[a[i]], c[a[i]]
      delete b[a[i]]
    }
  }
}
' | 
/usr/bin/sort -rn -t '/' -k 2,2 | /usr/bin/tr -d '/' | /usr/bin/head -n 15
)
carlo
  • 46
  • 1
  • When executing the above command from ZSH on Ubuntu 16.04 I got the following error `error: must set personality to get -x option`. The solution was to remove the `-` as `ps` argument. Executing with `/bin/ps amcwwwxo` worked perfectly! – Mandy Schoep Apr 28 '17 at 13:01
2

top cpu group by command line:

sudo ps aux | awk '{arr[$11]+=$3}; END {for (i in arr) {print arr[i],i}}' | sort -k1nr | head -n 10
diyism
  • 12,477
  • 5
  • 46
  • 46
0

It does not exist, you should write your own code/command/script to do it. Because all Google Chrome Helper processes are distinct processes, maybe you can write a script which computes all Chrome processes (helper, plug-in hosts etc.) using parent process id.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214