#!/bin/bash while :; do systemctl is-active kodi > /dev/null isRunning=$? # isrunning is 0, when kodi is running # echo "isrunning:" $isRunning " tvIsOn:" $tvIsOn # seaching for chromecast devices #avahi-browse -t -p -r _googlecast._tcp | grep -i chromecast | grep -q -i # in case of a fixed TV device IP adress if [ "$isRunning" -eq 0 ] ; then # the name of the TV needs to be configured in the home router # otherways use a fixed #i=360 i=5 while [ $i -gt 0 ] do # the bash has no build in command to decrease the loop counter, so we have to do a workaround i=$(python -c "import sys; print(int(sys.argv[1])-1)" $i ) ping -c 1 192.168.1.59 > /dev/null tvIsOn=$? if [ "$tvIsOn" -eq 0 ] ; then break else sleep 5 fi echo "Loop: $i" done echo "Left loop..." else TvCecStatus=$(echo pow 0 | cec-client -s -d 1 | grep 'power status' | awk '{split($0,a,":");print a[2]}' | sed 's/ //') if [ "$TvCecStatus" == "on" ]; then tvIsOn=0 else tvIsOn=1 fi fi if [ "$tvIsOn" -ne 0 -a "$isRunning" -eq 0 ] ; then date +"kodi stops at %c" >> /tmp/kodi.log systemctl stop kodi elif [ "$tvIsOn" -eq 0 -a "$isRunning" -ne 0 ] ; then date +"kodi starts at %c" >> /tmp/kodi.log systemctl start kodi fi sleep 10 done exit 0