Skip to content

Instantly share code, notes, and snippets.

@anandpathak
Created August 28, 2018 07:58
Show Gist options
  • Save anandpathak/d12f51e34eb91b7aaf7df52d754f20f9 to your computer and use it in GitHub Desktop.
Save anandpathak/d12f51e34eb91b7aaf7df52d754f20f9 to your computer and use it in GitHub Desktop.
monitor cpu and memory usage for long time
#!/bin/bash
DATE=$(date "+%Y-%m-%dT%H:%M:%S")
echo "total CPU, total Memory Usage, highest CPU process, Highest mem process " > stats-$DATE.csv
while :
do
cpu=`grep 'cpu ' /proc/stat | awk '{usage=($2+$4)*100/($2+$4+$5)} END {print usage "%"}'`;
memory=`free | grep Mem | awk '{print $3/$2 * 100.0}'`
top_cpu=`ps --sort=-pcpu -aux | sed -n 2p | awk '{print $3"|"$11}'`
top_mem=`ps --sort=-pcpu -aux | sed -n 2p | awk '{print $4"|"$11}'`
echo "$cpu,$memory,$top_cpu,$top_mem" >> stats-$DATE.csv
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment