Reputation: 1488
i have the following sh script, on a centos machine:
echo "PID,CPU,MEM,PROC"
while [ 1 ]
do
ps aux | grep mysql | awk '{print $2",", $3",", $4",", $11}'
sleep 1
done
the output every cycle has this output:
1163, 0.0, 0.0, /bin/sh
1265, 0.0, 1.5, /usr/libexec/mysqld
11807, 0.0, 0.3, grep
I wonder how to cycle the outputs and use variables to merge the value and return just one line: i don't need PID and PROC in the merged result
mysql, 0.0, 1.8
where 0.0 is CPU (all processes cpu usage sum) and 1.8 RAM (all processes ram usage sum).
Upvotes: 3
Views: 4886
Reputation: 75618
ps -eo "comm %cpu %mem" --no-headers | awk '{a[$1] = $1; b[$1] += $2; c[$1] += $3}END{for (i in a)printf "%s, %0.1f, %0.1f\n", a[i], b[i], c[i]}' | sort
Example output:
awk, 0.0, 0.0
bash, 0.0, 1.5
ps, 0.0, 0.0
sort, 0.0, 0.0
Process specific:
ps -C bash -o "comm %cpu %mem" --no-headers | awk '{a[$1] = $1; b[$1] += $2; c[$1] += $3}END{for (i in a)printf "%s, %0.1f, %0.1f\n", a[i], b[i], c[i]}'
Output:
bash, 0.0, 1.5
Upvotes: 4