Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use shell to get the memory and CPU utilization of a process name

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to use shell to obtain process name memory and CPU utilization" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "how to use shell to obtain process name memory and CPU utilization" article can help you solve the problem.

Principle:

1) use ps to get the process name of all processes pid

Ps-C process_name-o pid=

2) use / proc/$pid/status to get the memory footprint and loop the sum

3) use top to get the cpu list of the relevant pid, and filter the 10th column to sum.

Test on ubuntu18.04 and centos7.9: #! / bin/bash# author:robin# usage: bash proc.sh openrestyif [- e cpu_mr.log] then rm-f cpu_mr.logfi pid_array= name=$1if [- z "$name"] then name= "openresty" fi#echo $name # cpucmd:'- p 125-p 235-p 568'cpucmd=''i=0 # get all pid by proNamefunGetPids () {tempstr= `ps-C $name-o pid= `# echo $tempstr For line in $tempstr do pid_array [$I] = $line # echo ${pid_array [$I]} let "iTunes +" cpucmd=$ {cpucmd} "- p $line" done return} funGetPids if [$I-eq 0] thenecho "{\" sum_vmrss\ ":-1 \ "sum_cpu\":-1} "exit 0fi sum_vmrss=0sum_cpu=0.0#compute mem sumfor pid in ${pid_array [@]} do vmrss_temp= `cat / proc/$pid/status | grep-e VmRSS | tr-d [: blank:] [: alpha:] [:] ``# echo 'vmrss_temp=' $vmrss_temp sum_vmrss= `expr $sum_vmrss + $vmrss_ temp`done # echo $cpucmdtempstr= `top-n 1$ cpucmd | tail-n + 8 | awk'{print $10} '`for line in $tempstrdo#echo $linesum_ Cpu=$ (echo "$sum_cpu+$line" | bc) done # echo "-" echo "{\" sum_vmrss\ ": $sum_vmrss \ "sum_cpu\": $sum_cpu} "exit usage: bash proc.sh gnome-shell

Output json:

{"sum_vmrss": 423712, "sum_cpu": 6.7}

This is the end of the content about "how to use shell to get the memory and CPU utilization of the process name". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report