1

I want to list main applications processes report in this format

ps -e -o pid,comm,pmem,pcpu,uname

To filter main GUI applications wmctrl -pl is the only way I can get processes ids.its great if xlsclients can be used. It contains the real list with names I want.

How can I combine them as single commands to get report as I want. (ps -p = xlsclients -p)

Braiam
  • 35,991

2 Answers2

2

It's often the case that in Unix you can chain commands together, and often times many commands are built specifically so that they'll work with the output generated by other commands.

Luckily you can take the output of xlsclients and parse it down so that it's just the name of the command. You can then pass this info to the ps command to get the output you're looking for:

$ xlsclients
dufresne  conky -c /home/slm/.conky/b.conf
dufresne  cinnamon-session
dufresne  cinnamon-settings-daemon
dufresne  csd-locate-pointer
dufresne  clipit

$ xlsclients | cut -d" " -f3 | paste - -s -d ','
conky,cinnamon-session,cinnamon-settings-daemon....

You can then give this list of process names to the -C switch of ps.

$ ps -o pid,comm,pmem,pcpu,uname \
    -C $(xlsclients | cut -d" " -f3 | paste - -s -d ',')

NOTE: We've removed the -e switch since we're now providing a list to ps.

Example

$ ps -o pid,comm,pmem,pcpu,uname \
    -C "$(xlsclients | cut -d" " -f3 | paste - -s -d ',')" | head 
  PID COMMAND         %MEM %CPU USER
 1998 cinnamon-launch  0.2  0.0 slm
 2031 cinnamon         6.5  0.7 slm
16736 cinnamon-launch  0.3  0.0 slm
16738 cinnamon         6.1  2.7 slm
16994 cinnamon-sessio  0.2  0.0 slm
17231 cinnamon-settin  0.4  0.0 slm
17293 csd-locate-poin  0.2  0.0 slm
17331 nm-applet        0.3  0.0 slm
17339 clipit           0.2  0.1 slm
slm
  • 369,824
  • thanks, But how to get the sum of same command group, without repeat them. cinnamon %MEM => 6.5+6.1 = 12.6, %CPU => 0.7+2.7 = 3.4 – Oshan Wisumperuma Jul 29 '14 at 14:07
  • ok, i fixed it from awk. ps -o pid,comm,pmem,pcpu,uname -C "$(xlsclients | cut -d" " -f3 | paste - -s -d ',')" | awk 'BEGIN{FS=" "; print "PID COMMAND %MEM %CPU count"} NR!=1 {comm[$2]++;pid[$2]=$1;pid[$2]=$1;mem[$2]=mem[$2]+$3;cpu[$2]=cpu[$2]+$4}END{for (i in comm) printf("%d %s %5.2f %5.2f %d\n",pid[i],i,mem[i],cpu[i],comm[i])}' – Oshan Wisumperuma Jul 29 '14 at 17:03
0

$ ps -o pid,comm,pmem,pcpu,uname \ -C "$(xlsclients | cut -d" " -f3 | paste - -s -d ',')" | head PID COMMAND %MEM %CPU USER 1998 cinnamon-launch 0.2 0.0 slm 2031 cinnamon 6.5 0.7 slm 16736 cinnamon-launch 0.3 0.0 slm 16738 cinnamon 6.1 2.7 slm 16994 cinnamon-sessio 0.2 0.0 slm 17231 cinnamon-settin 0.4 0.0 slm 17293 csd-locate-poin 0.2 0.0 slm 17331 nm-applet 0.3 0.0 slm 17339 clipit 0.2 0.1 slm