Skip to content

Instantly share code, notes, and snippets.

@HYChou0515
Created September 6, 2019 13:17
Show Gist options
  • Save HYChou0515/5dd91207d28e4a035e71b8070a7ac9dc to your computer and use it in GitHub Desktop.
Save HYChou0515/5dd91207d28e4a035e71b8070a7ac9dc to your computer and use it in GitHub Desktop.
A simple cpu tracker
#!/bin/bash
# This script check average cpu usage and send an email if the usage is low.
# A use case is to make better usage of a sharing machine.
# This script needs send_mail.py at https://gist.github.com/HYChou0515/fc152ce932024b29092be234863928ba#file-send_mail-py
function cpu_usage()
{
top -bn2 | grep "Cpu(s)" | tail -1 | \
sed "s/.*, *\([0-9]*\)[0-9.]*%* id.*/\1/" | \
awk '{print 100 - $1}'
}
CPU_BOUND=50
MAX=10
counter=$MAX
while true
do
sleep 600
cpu=$(cpu_usage)
echo $cpu"%"
if [ $cpu -le $CPU_BOUND ]
then
counter=$((counter-1))
if [ $counter -le 0 ]
then
echo cpu usage too low
cat code | ./send_mail.py s1243221@gmail.com mail.pub "CPU ($cpu%) usage is lower than $CPU_BOUND% at $(date)"
exit 1
fi
else
counter=$MAX
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment