Kodi Shutdown Script

I am currently using 14.2 of Kodi on my home media center. In order to save money and energy, our rule of thumb is to keep the media center powered off during the night. At first thought, it would make sense to use Kodi’s built-in sleep function; however, it gets too mundane to open the setting option every time you want to watch something on the media center. Our second choice was to install a cron job to shutdown every night before we went to sleep. Our trouble with this aspect is that while we normally would be in bed before 2230, sometimes our kids would not fall asleep until 2100. This would make our together time begin later than normal. If we chose to watch a movie in our together time, we often found out we had to restart our media center just to get the last 5 minutes of the movie.

This led to our third and optimal solution: find a script to do the job or make one. I searched and could not find an already made script that did what I want:

  1. Initiate a shutdown at a certain time
  2. Do not interrupt viewing when a video is playing past the certain time

With those criteria, I was able to make the following bash script and placed it in my crontab:

#!/bin/bash
#set up curl env
#username= #good idea to have one
#password=#good idea to have one
baseurl=http://127.0.0.1:8080/jsonrpc
curl_cmd="curl --user ${username}:${password} --silent " #remove --user ${username}:${password} if you do not have one set up

#Retun playerid
#for the json command to work with kodi, the json format has to be '{"key":"value"}' 
${curl_cmd} -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}' $baseurl > /tmp/kodiOutput

#if playerid is found, run the scripts, otherwise, shutdown now
if grep --quiet playerid /tmp/kodiOutput; then
  #Get necessary items 
	${curl_cmd} -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "method": "Player.GetProperties", "params": { "playerid": 1, "properties": ["time"] }, "id": 1}' $baseurl > /tmp/kodiCurrent.json
	${curl_cmd} -H "Content-Type: application/json" --data '{"jsonrpc": "2.0", "method": "Player.GetProperties", "params": { "playerid": 1, "properties": ["totaltime"] }, "id": 1}' $baseurl > /tmp/kodiTotal.json

	#Convert time
	CHOURS=$[$(cat /tmp/kodiCurrent.json | awk -F: -v RS="," '/"hours"/ {print $4}')*60]
	CMINS=$[$(cat /tmp/kodiCurrent.json | awk -F: -v RS="," '/"minutes"/ {print $2}')]
	THOURS=$[$(cat /tmp/kodiTotal.json | awk -F: -v RS="," '/"hours"/ {print $4}')*60]
	TMINS=$[$(cat /tmp/kodiTotal.json | awk -F: -v RS="," '/"minutes"/ {print $2}')+5] #adding 5 minutes to the shutdown time to overlap for remaining seconds and milliseconds that may be present.
	REMAININGSECONDS=$[$[$THOURS+$TMINS]-$[$CHOURS+$CMINS]]
        sleep $REMAININGSECONDS 
        sleep 300
        $0
else
	shutdown -h now &
fi

Required programs:

  • curl
  • awk
  • cat
  • grep

Known Limitations

  • The Kodi web interface has to be enabled.
  • You have 5 minutes after the script was launched from cron to queue another video.