Direkt zum Hauptbereich

Wait till hdmi is connected before starting kodi... and poll tvheadend for activity before shutdown

What?

 If you run tvheadend and kodi on one device you want to wait to start kodi until hdmi is conected,

Why?

kodi has bugs that cause issues when started before connected to hdmi

  • Sometimes it crashes
  • Sometimes it stops to render text in menu

How to work arround?

Wait for HDMI and timeout if no recording is upcomming

#!/bin/bash

# This scripts wait <TIMEOUTINSECONDS> till hdmi connection got established or shutdown if no tvheadend recording is running, upcomming or some stream is consumed, served by tvheaden
# Howto https://debianisttoll.blogspot.com/2021/07/wait-till-hdmi-is-connected-before.html
# Source https://gist.githubusercontent.com/stesee/9ebb11cc2db6a58e6b42afb2d74e7e7b/raw/7327e42950c449c1153ebf546f60ee1d8e3d0d0d/waitTillHdmiConnected.sh

# install stuff before using this script
#   sudo apt install libxml-xpath-perl curl

# setup systemconfiguration
#   allow current user to execute shutdown and waskup - add this line to /etc/sudoers - replace "username" with your username
#       username ALL = NOPASSWD: /usr/bin/setwakeup.sh, /usr/sbin/pm-suspend

# configure your settings

WARNINGTIMEOUTINSECONDS=800
TIMEOUTINSECONDS=900
POLLINGDURATIONINSECONDS=5
LOGFILE="$HOME/waitTillHdmiConnected.log"

# configure your tvheadend settings
URL=localhost
PORT=9981
USERNAME=kodi
PASSWORD=kodi
# Time this script will wait without shutting down for a planned recording
TIMEBEFORENEXTRECORDINMINUTES=20

#set start conditions, is this some bash magic? seems like SECONDS will be increased by every second without coding anything
SECONDS=0

triggerTimeoutActions(){
    #set next wakeup time
    OUTPUT=$(curl --anyauth -u $USERNAME:$PASSWORD --silent --max-time 5 "http://$URL:$PORT/status.xml")
    NEXTRECORD=$(echo "$OUTPUT| xpath -q -e "/currentload/recordings[1]/recording/next/text()")
    NEXTRECORD_TICKS=$(date '+%s' -d "+ $NEXTRECORD minutes")
    
    #TODO Add sanity check value and fall back to someting reasonable... e.g. 24h in future
    FAILSAFE=`date '+%s' -d '+ 24 hours'`
    #TODO Add sanity check value and fall back to someting reasonable... e.g. 24h in future
    
    sudo /usr/bin/setwakeup.sh "$NEXTRECORD_TICKS"
    
    # e.g. shutdown the system
    echo "Shuting down next wakup set to $NEXTRECORD_TICKS" | tee -a "$LOGFILE"
    echo $(date && echo "shutting realy down") | tee -a "$LOGFILE"
    shutdown +1 | tee -a "$LOGFILE"
    exit
}

triggerHdmiConnectedActions(){
    # e.g. set some overscan hack
    #xrandr --output HDMI-3 --transform 1.05,0,-34,0,1.05,-20,0,0,1
    # e.g. start kodi
    kodi &
    exit
}

isHdmiPortConnected(){
    if xrandr 2> /dev/null | grep -q 'HDMI-[0-9] connected'then
        echo "HDMI connected" | tee -a "$LOGFILE"
        return 0
    else
        echo "HDMI not connected waiting $POLLINGDURATIONINSECONDS seconds" | tee -a "$LOGFILE"
        return 1
    fi
}

isTvheadenRecordingActive(){
    # check if tvheadend is responding
    OUTPUT=$(curl --anyauth -u $USERNAME:$PASSWORD --silent --max-time 5 "http://$URL:$PORT/status.xml")
    if [ -z "$OUTPUT" ]; then
        echo "Tvheadend is not responding" | tee -a "$LOGFILE"
        return 1
    else
        # check if tvheadend is recording
        if echo "$OUTPUT" | grep "<status>Recording</status>" >/dev/null; then
            echo "Tvheadend is recording" | tee -a "$LOGFILE"
            return 0
        fi
        
        # check if tvheadend recording is upcomming in TIMEBEFORENEXTRECORDINMINUTES
        if echo "$OUTPUT" | grep "<next>" >/dev/null; then
            echo "Tvheadend waiting for an upcomming recording" | tee -a "$LOGFILE"
            echo "$OUTPUT"
            NEXTRECORD=$(echo "$OUTPUT| xpath -q -e "/currentload/recordings[1]/recording/next/text()")
            echo Next recording starts in "$NEXTRECORD" minutes | tee -a "$LOGFILE"
            if [ "$TIMEBEFORENEXTRECORDINMINUTES" -gt "$NEXTRECORD" ]; then
                echo "Tvheadend will start to record in short time" | tee -a "$LOGFILE"
                return 0
            fi
        fi
        
        # check if tvheadend rcording serving some stream
        if echo "$OUTPUT" | grep "<subscriptions>" >/dev/null; then
            SUBS=$(echo "$OUTPUT| xpath -q -e "/currentload/subscriptions/text()")
            if [ "$SUBS" -gt 1 ]; then
                echo "$SUBS subscriptions running" | tee -a "$LOGFILE"
                return 0
                elif [ "$SUBS" -eq 0 ]; then
                echo "No subscriptions" | tee -a "$LOGFILE"
            fi
        fi
    fi
    return 1
}

echo $(date && echo "Start polling") | tee -a "$LOGFILE"

while true
do
    if isHdmiPortConnected; then
        triggerHdmiConnectedActions
    fi
    
    duration=$SECONDS
    
    if isTvheadenRecordingActive; then
        SECONDS=0
    else
        echo "$(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed since first poll." | tee -a "$LOGFILE"
        if [ "$duration" -gt "$WARNINGTIMEOUTINSECONDS" ]; then
            echo "Warning: HDMI connection timeout is about to occure!" | tee -a "$LOGFILE"
            if [ "$duration" -gt "$TIMEOUTINSECONDS" ]; then
                triggerTimeoutActions
            fi
        fi
    fi
    
    sleep $POLLINGDURATIONINSECONDS
done

Allow shutdown

Add this to /etc/sudoers

htpc ALL = NOPASSWD: /usr/bin/setwakeup.sh, /usr/sbin/pm-suspend 

To lookhup how to create /usr/bin/setwakeup.sh see https://debianisttoll.blogspot.com/2021/06/send-to-sleep-and-wake-up-tvheadend.html



Kommentare

Beliebte Posts aus diesem Blog

Kodi keeps crashing when it is started without connected displays. Sometimes I want to startup my kodi setup for some other reasen and start using kodi later.... then kodi was already closed with some crash log file... :( This script works arround by waiting to start kodi until some hdmi port is connected (modify the grep query to adapt to some other port name). #!/bin/bash # Some programs have issues when run without connected display. E.g. Kodi likes to crash when started without any display connected on my system. # This script starts after some hdmi port is connected. # Depends on xrandr. WARNINGTIMEOUTINSECONDS=840 TIMEOUTINSECONDS=900 POLLINGDURATIONINSECONDS=5 LOGFILE= " $HOME /waitTillHdmiConnected.log" #set start conditions SECONDS=0 triggerTimeoutActions (){      echo   ...

Skript kiddies und SSH

Anzahl an fehlerhaften Logins ausgeben: cat /var/logauth.log | grep invalid | wc -l Für einen Server, der in halbes Jahr im Netz läuft finde ich 68350 Versuche. Ein Auszug von /var/logauth.log Jun 3 00:12:02 uhweb69144 sshd[11470]: Invalid user hilary from 201.147.235.91 Jun 3 00:12:02 uhweb69144 sshd[11470]: reverse mapping checking getaddrinfo for static.customer-201-147-235-91.uninet-ide.com.mx failed - POSSIBLE BREAK-IN ATTEMPT! Jun 3 00:12:02 uhweb69144 sshd[11470]: (pam_unix) check pass; user unknown Jun 3 00:12:02 uhweb69144 sshd[11470]: (pam_unix) authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost= 201.147.235.91 Jun 3 00:12:04 uhweb69144 sshd[11470]: Failed password for invalid user hilary from 201.147.235.91 port 49507 ssh2 Jun 3 00:12:09 uhweb69144 sshd[11530]: Invalid user howard from 201.147.235.91 Jun 3 00:12:09 uhweb69144 sshd[11530]: reverse mapping checking getaddrinfo for static.customer-201-147-235-91.uninet-ide.com.mx failed - POSS...

favourite geek&poke comics

http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef013485e5e37b970c-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef013486458e35970c-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0133f32a0f25970b-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0133f3716c17970b-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef013486997ef7970c-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef013487146609970c-pi http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0133f4227ee4970b-pi