Last active
April 19, 2020 18:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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