[HowTo] for auto quit/start kodi based on TV status

I was having a problem with this script that could be reproduced by the following steps:
Turn off TV (Kodi shutsdown)
Wait sometime, like 15 minutes
Turn on TV (Kodi process starts)
Screen stays black

I assumed that this was caused by some power management option and because of that I explicitly turn on and off the screen in the script

#!/bin/bash

oldcmd=" "
while :
do
        cmd=$(/opt/vc/bin/tvservice -n 2>&1)
        if [[ $cmd == *'No device present'* && $cmd != $oldcmd ]]
        then
                echo stopping Kodi
                #Turn off Kodi
                $(sudo systemctl stop mediacenter)
                #Turn off HDMI (tvservice -n still works)
                /opt/vc/bin/tvservice -o
                oldcmd=$cmd
        elif [[ $cmd != $oldcmd ]] ; 
	then
                echo starting Kodi
                #Turn on HDMI
                /opt/vc/bin/tvservice -p
                #Turn on Kodi
                $(sudo systemctl start mediacenter)
                oldcmd=$cmd
        fi

        echo $cmd
        sleep 10
done
1 Like