[英]Bash Script that gets a count of running processes and then emails if over a certain threshold
我正在嘗試創建一個腳本來獲取當前正在運行的進程數,如果該數字超過1000,則向我發送一封電子郵件。 我試圖在bash腳本中執行此操作,我將使用cron作業來調用它。 我正在使用的代碼如下,我確信我只是有一些不合適的東西,只需要另一組眼睛。
PCOUNT=$(cat /proc/loadavg|awk '{print $4}'|awk -F/ '{print $2}')
if [$PCOUNT > 100]; then
mail -s "Process Count" email@example.com
fi
您的if
語句中的上下文不正確。 嘗試(( ))
而不是[]
您if
下面像圖所示:
PCOUNT=$(cat /proc/loadavg|awk '{print $4}'|awk -F/ '{print $2}')
if (( $PCOUNT > 100 )); then
mail -s "Process Count" email@example.com
fi
注意:我的系統上沒有mail
設置,因此無法驗證mail
命令。
你可以減少awk
awk -F“| /”'{print $ 5}'/ proc / loadavg
如果條件
[$ PCOUNT -ge 1000
郵件,傳遞$ PCOUNT
mail -s“進程數:$ PCOUNT”
恕我直言,如果這是警告為什么不嘗試nagios插件
經過大量的反復試驗,我終於找到了解決方案。 我最終獲取了awk語句的輸出並將其寫入文件。 然后我抓住文件並將輸出發送到郵件。
PCOUNT=$(awk -F" |/" '{print $5}' /proc/loadavg)
if ((PCOUNT>1000)); then
echo "Number of Running Processes is" $PCOUNT >>/tmp/mail.txt
cat /tmp/mail.txt | mail -s "Number of processes is rising" example@email.com
fi
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.