[英]Combine rows of Bash “ps” by command
How to combine processes of the same program in Linux Bash when using ps
, top
or htop
? 使用ps
, top
或htop
时,如何在Linux Bash中合并同一程序的htop
?
For example, when called ps -eo pmem,pcpu,args
instead of this: 例如,当调用ps -eo pmem,pcpu,args
代替此命令时:
...
2.0 1.0 /usr/sbin/apache2 -k start
3.0 2.0 /usr/sbin/apache2 -k start
5.0 1.0 /usr/sbin/apache2 -k start
2.5 1.0 /usr/sbin/mysqld
...
it would show 它会显示
...
10.0 4.0 /usr/sbin/apache2 -k start
2.5 1.0 /usr/sbin/mysqld
...
with memory and CPU values summed. 与内存和CPU值相加。
Maybe there is another command to achieve this? 也许还有另一个命令可以实现这一目标?
awk '{m=$1; c=$2; $1=$2=""; pmem[$0]+=m; pcpu[$0]+=c} END{for(i in pmem) {printf("%5.1f %5.1f %s\n",pmem[i], pcpu[i], substr(i,3))}}' file
Output: 输出:
10.0 4.0 /usr/sbin/apache2 -k start 2.5 1.0 /usr/sbin/mysqld
With some comments: 有一些评论:
awk '{m=$1; c=$2 # save column 1 and 2
$1=$2="" # remove content of columns 1 and 2
pmem[$0]+=m; pcpu[$0]+=c} # save memory and cpu to hashes and
# add to its value, use rest of
# row as key
# print content of both hashes and key in a loop
END{for(i in pmem) {printf("%5.1f %5.1f %s\n",pmem[i], pcpu[i], substr(i,3))}}' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.