Wednesday, 25 May 2016

Bash Script to find the exact application or command which consumed high CPU and Memory

#!/bin/bash
#loadavg=$(awk '{ print $1 "\t" $2 "\t" $3 }' /proc/loadavg )
#To find the exact memory used percentage[Used-(Cached+Buffer)]
Memusage=$(printf '%.0f\n' `free | awk 'FNR == 3{print $3/($3+$4)*100}'`)
now=$(date +"%Y-%m-%d %H:%M:%S")
#To find the cpu load average
cpu_load=$(printf '%.0f\n' `uptime | awk '{print $(NF-2)}' | cut -c1-4`)
if [ "$cpu_load" -ge "25" ] || [ "$Memusage" -ge "50" ];then
  echo -e "\n\n=================\t$now\t========================" >> /var/log/top_report
  top -b -c -n1 >> /var/log/top_report
fi

No comments:

Post a Comment