Skip to content

Instantly share code, notes, and snippets.

@jaworek
Last active April 19, 2020 18:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaworek/111c140cde95478d7984f0ca14dd398f to your computer and use it in GitHub Desktop.
Save jaworek/111c140cde95478d7984f0ca14dd398f to your computer and use it in GitHub Desktop.
Automatically shutdown serwer when usage is lower than 25% for at least 30 minutes.
#!/bin/sh
BASEDIR=$(dirname "$0")
file="$BASEDIR/inactivity_count.txt"
if [ ! -e $file ]
then
touch $file
echo 0 > $file
fi
cpu_usage=$( ps -A -o %cpu | awk '{s+=$1} END {print s }' | awk -F. '{print $1}' )
counter=`cat $file`
cpu_treshold=26
if [ $cpu_usage -lt $cpu_treshold ]
then
echo $(($counter + 1)) > $file
else
echo 0 > $file
fi
if [ $counter -gt 30 ]
then
echo "Shutting down server due to inactivity."
echo 0 > $file
sudo shutdown
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment