Create a gist now

Instantly share code, notes, and snippets.

@fbartels /shutdown-on-not-used Secret
Last active Dec 22, 2015

What would you like to do?
#!/bin/sh
LOGFILE="/tmp/shutdown-script.log"
#LOGFILE=/dev/null
COUNTFILE=/tmp/shutdown-counter
log() {
echo `date +%c` $1 >> $LOGFILE
}
cancel() {
[ -f $COUNTFILE ] && rm $COUNTFILE
sleep 540
exit 0
}
##########################################
# Hier ist Platz für die einzelnen Checks
##########################################
# Terminate early if stopfile exists
STOPFILE=/tmp/shutdown-no
if [ -e $STOPFILE ]; then
log "Stopfile exists. Doing nothing."
cancel
fi
# Timecheck
uptime=$(cat /proc/uptime)
uptime=${uptime%%.*}
minutes=$(( uptime/60 ))
if [ $minutes -lt 30 ]; then
log "Online since only $minutes minutes. Doing nothing."
cancel
fi
# Check if synolocalbkp is running
if [ "$(pidof synolocalbkp)" ]; then
log "Backup is running"
cancel
fi
# Check if there is a connection via Webinterface or in of the Apps
if netstat | grep 'fozzie:https\|fozzie:5000\|fozzie:5001\|fozzie:5006' | grep ESTABLISHED > /dev/null; then
log "Active connection to HTTPS, WebDAV or other DSM App"
cancel
fi
# Check if transmission is downloading
USER=username
PASSWORD=password
TRANSMISSION="/usr/local/transmission/bin/transmission-remote --auth "$USER":"$PASSWORD
if $TRANSMISSION -l | grep 'Downloading\|Up & Down' > /dev/null; then
log "Transmission currently downloading"
cancel
fi
# Check if SABnzbd is downloading
URL=localhost
PORT=8080
API=xxxx
if curl --silent "http://$URL:$PORT/api?mode=qstatus&output=json&apikey=$API" | grep Downloading > /dev/null; then
log "SABnzbd is downloading"
cancel
fi
# Check if one of the ACTIVEHOSTS has an open connection
ACTIVEHOSTS=""
for host in $ACTIVEHOSTS ; do
if netstat -n | grep ' '$host':.*ESTABLISHED' > /dev/null; then
log "$host currently accessing NAS"
cancel
fi
done
# Check if Tvheadend is recording
URL=192.168.178.9
PORT=9981
USERNAME=xxx
PASSWORD=xxx
OUTPUT=$(curl -u $USERNAME:$PASSWORD --silent --max-time 5 "http://$URL:$PORT/status.xml")
if [ -z "$OUTPUT" ]; then
log "tvheadend ist not responding."
#log "Tvheadend is not responding, restarting it."
#kill -9 $(pidof tvheadend)
#sleep 5
#/var/packages/tvheadend/scripts/start-stop-status start
elif echo $OUTPUT | grep "<status>Recording</status>" > /dev/null; then
log "Tvheadend is recording"
cancel
elif ! echo $OUTPUT | grep "<subscriptions>0</subscriptions>" > /dev/null; then
log "Tvheadend is active"
cancel
fi
till=$(echo $OUTPUT | grep next | sed -e 's,.*<next>\([^<]*\)</next>.*,\1,g')
if [ -z "${till##*[!0-9]*}" ]; then
log "No recording sheduled"
elif [ $till -lt 90 ]; then
log "Next recording starts in $till minutes. Doing nothing."
cancel
fi
# Pingcheck - should be performed last
PINGHOSTS="waldorf astoria"
for host in $PINGHOSTS ; do
if ping -c 1 -w 1 $host > /dev/null; then
log "$host isn't offline"
cancel
fi
done
##########################################
# und vorbei
##########################################
# Increment counter if all checks failed
echo >>$COUNTFILE
COUNTER=`ls -la $COUNTFILE | awk '{print $5}'`
log "NAS has been idle for $COUNTER checks"
# Shutdown NAS if counter has already been incremented 10 times
if [ $COUNTER -gt 10 ]; then
log "updating power on time for tvheadend"
/root/pvr-poweron.py > /etc/power_sched.conf
log "shutdown Diskstation"
rm $COUNTFILE
/sbin/poweroff
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment