Skip to content

Instantly share code, notes, and snippets.

@jodiecunningham
Created August 9, 2015 15:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jodiecunningham/5466f1344b9b8c23c401 to your computer and use it in GitHub Desktop.
Save jodiecunningham/5466f1344b9b8c23c401 to your computer and use it in GitHub Desktop.
Shut down idle AWS plex instance
#!/bin/bash
#set -x
#DEBUG=echo
# Runs via cron to shut down the instance if it's not in use for 5 minutes
# Starts checks after it's been up more than 500secs
# Won't kick you out if you're ssh'ed in and it's idle.
# crontab -l|grep idle
# */6 * * * * /root/bin/idle.sh >/dev/null 2>&1
until $(awk '$1>500{exit 1}' /proc/uptime)
do
if [ $(sar -n DEV 300 1|awk '/eth0/{ if ($11<0.05){ exit 1;} exit 0; }') ] || [ $(ss -o state established '( sport = :ssh )'|wc -l) = 1 ];
then
echo "$(date) Shutting down" >>/root/bin/idle.log;
${DEBUG} /sbin/poweroff;
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment