How to implement shell script code for counting cpu memory and usage
This article mainly introduces how to achieve statistics of cpu memory and utilization of shell script code, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
The code is as follows:
#! / bin/sh
Clear
While ((1 > 0))
Do
NET=$ (netstat-anp | grep 1000 | grep EST | wc-l)
PRC=$ (ps aux | grep thread1 | grep-v grep | grep-v SCREEN | awk'{print "CPU:" $3 "%, MEM:" $4 "%, RSS:" $6max 1024 "M, VSZ:" $5pm 1024 "M, PID:" $2 ""}')
Tput sc; echo-n-e "\ b"; tput rc
Echo "SRV: thread1, NET: $NET, $PRC"
Echo ""
NET=$ (netstat-anp | grep 1011 | grep EST | wc-l)
PRC=$ (ps aux | grep thread2 | grep-v grep | grep-v SCREEN | awk'{print "CPU:" $3 "%, MEM:" $4 "%, RSS:" $6max 1024 "M, VSZ:" $5pm 1024 "M, PID:" $2 ""}')
Tput sc; echo-n-e "\ b"; tput rc
Echo "SRV: thread2, NET: $NET, $PRC"
Echo ""
NET=$ (netstat-anp | grep 1022 | grep EST | wc-l)
PRC=$ (ps aux | grep thread3 | grep-v grep | grep-v SCREEN | awk'{print "CPU:" $3 "%, MEM:" $4 "%, RSS:" $6max 1024 "M, VSZ:" $5pm 1024 "M, PID:" $2 ""}')
Tput sc; echo-n-e "\ b"; tput rc
Echo "SRV: thread3, NET: $NET, $PRC"
Echo ""
NET=$ (netstat-anp | grep 1033 | grep EST | wc-l)
PRC=$ (ps aux | grep thread4 | grep-v grep | grep-v SCREEN | awk'{print "CPU:" $3 "%, MEM:" $4 "%, RSS:" $6max 1024 "M, VSZ:" $5pm 1024 "M, PID:" $2 ""}')
Tput sc; echo-n-e "\ b"; tput rc
Echo "SRV: thread4, NET: $NET, $PRC"
Echo ""
NET=$ (netstat-anp | grep beam.smp | grep EST | grep 222.222.18.222 NET=$ 800 | wc-l)
PRC=$ (ps aux | grep thread | grep-v grep | awk'{CPU + = $3} {RSS + = $6} {VSZ + = $5} END {print "CPU:" CPU "%, MEM:" MEM "%, RSS:" RSS/1024 "M, VSZ:" VSZ/1024 "M,"}'| awk 'END {print}')
Tput sc; echo-n-e "\ b"; tput rc
Echo "SRV: All, NET: $NET, $PRC"
Echo ""
Sleep 0.5
Tput cup 0 0
Done
Thank you for reading this article carefully. I hope the article "how to realize the shell script code for counting cpu memory and usage" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!