[精讚] [會員登入]
181

使用bash script 顯示及比較記憶體剩餘用量

How to get the available memory reported as a percentage then compare Numbers and Strings in Linux Shell Script

分享此文連結 //n.sfs.tw/15730

分享連結 使用bash script 顯示及比較記憶體剩餘用量@外行中的外行
(文章歡迎轉載,務必尊重版權註明連結來源)
2022-05-07 10:57:16 最後編修
2022-04-04 23:42:12 By jung
 

紀錄一下,離失憶症越來越近的一天

一個月前,由於wildfly時常因為記憶體不足導致效能低下

所以翻了很多網路文章,找到使用bash顯示及比較記憶體的方法

結果一個月後,完全看不懂自己在寫什麼了

所以...

方法一:直接使用awk處理後,將文字String轉為數值比較大小,需要bc這個函式庫

FREEM=`free | grep Mem | awk '{print $3/$2 * 100.0}'`
echo $FREEM
if [ "`echo "${FREEM} > 50" | bc`" -eq 1 ];
        then
                echo "memory usage is ok"
        else
                echo "wildfly need to restart"
                systemctl restart wildfly
fi

awk處理後數值換成文字了,所以要傳給bc去處理,才能使用數學運算式

最後用 -eq 1 來比較前面運算後的結果boolean值 0 為 假(false),1 為 真(true)

如果結果為真(1),就跳去if else判斷式,這樣也行

方法二:發現直接在awk把結果變成數值就好?

musage=$(free | awk '/Mem/{printf("RAM Usage: %.2f%\n"), $3/$2*100}' |  awk '{print $3}' | cut -d"." -f1)
if [ $musage -le 56 ]; then
echo "Current Memory Usage: $musage%"
systemctl stop httpd && systemctl restart wildfly && systemctl start httpd
else
echo "Memory usage is: $musage% ,under threshold"
fi

重點在經過cut -d 和 -f 就變成數值了?!

所以直接用 -le 比較結果

終於知道這些參數option是什麼意思Orz

我好廢啊!!

  • num1 -eq num2                  check if 1st  number is equal to 2nd number
  • num1 -ge num2                  checks if 1st  number  is greater than or equal to 2nd number
  • num1 -gt num2                  checks if 1st  number is greater than 2nd number
  • num1 -le num2                   checks if 1st number is less than or equal to 2nd number
  • num1 -lt num2                   checks if 1st  number  is less than 2nd number
  • num1 -ne num2                  checks if 1st  number  is not equal to 2nd number

以下參考資料

大概是我的問題太嫩,stackoverflow居然沒有這些作法

https://www.geeksforgeeks.org/bc-command-linux-examples/

https://unix.stackexchange.com/questions/490720/how-to-convert-a-string-to-a-float-and-then-use-it-in-a-conditional-operation

https://stackoverflow.com/questions/10585978/how-to-get-the-percentage-of-memory-free-with-a-linux-command

https://www.linuxtechi.com/compare-numbers-strings-files-in-bash-script/

END

你可能感興趣的文章

使用群暉NAS內建VPN SERVER套件的OPENVPN功能注意事項 使用群暉NAS內建VPN SERVER套件的OPENVPN功能注意事項

在virtualbox如何壓縮vdi檔 using commands to reduce virtualbox's vdi file space

啟動chrome時要求須輸入密碼enter password to unlock login keyring asking for entering password to unlock login kerying on Ubuntu desktop

在ubuntu18.04桌面版安裝中文注音輸入法 如何在ubuntu桌面使用新酷音注音輸入法

設定win10開機自動執行openvpn連接vpn server how to make win10 connecting to a vpn server automactically after booting up

zfs on ubuntu 20.04 how to destroy zfs pool while error message said pool is busy

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[網管心得]ssh keygen用法 ssh-copy-id

nginx+ssl+certbot安裝設定雜記 using certbot on nginx to setup https connection

[NetBeans]如何從git下載專案改建為自己的專案 修改git專案

IIS10設定url rewrite to https if you setup url rewrite function in IIS10, might cause api callback failed

intellij IDEA git設定說明 whenever using git push fuction in the intellij IDEA