Ubuntu – Prevent shutdown when tvheadend recording / KDE session is active


I am searching for a way to prevent shutdown/standby when tvheadend (or vdr) is currently recording.

What I found so far:

I see two solutions:

  • Either TVHeadend script calls some KDE functions to ask for KDE state (screen locked, user logged in etc) and issues shutdown/suspend calls as seen in the above example.
  • or modify the shutdown behavior of KDE so it checks if any TVH recording is currently active.

I cannot find any docs about these two variants.

Best Answer

Found a really easy solution!
Create a file /etc/pm/sleep.d/70-check-recordings.

In this file check if a recording currently is active. If yes, just return exit 1. That's it. Suspend will be aborted.

My script looks like this, it is based on this at tvheadend.

$ cat /etc/pm/sleep.d/00-check-recordings
# this script sets ACPI Wakeup alarm and stops standby if a recording is active
# safe_margin - minutes to start up system before the earliest timer

# bootup system x sec. before timer

# modify if different location for tvheadend dvr/log path
cd ~hts/.hts/tvheadend/dvr/log



current_date=`date +%s`

for i in $( ls ); do
        tmp_start=`cat $i | grep '"start":' | cut -f 2 -d " " | cut -f 1 -d ","`
        tmp_stop=`cat $i | grep '"stop":' | cut -f 2 -d " " | cut -f 1 -d ","`
#       logger "$0: $i from $(date -d @$tmp_start) to $(date -d @$tmp_stop)"

        start_extra=`cat $i | grep '"start_extra":' | cut -f 2 -d " " | cut -f 1 -d ","`
        stop_extra=`cat $i | grep '"stop_extra":' | cut -f 2 -d " " | cut -f 1 -d ","`

        let tmp_start=$tmp_start-$start_extra*60
        let tmp_stop=$tmp_stop+$stop_extra*60
#       logger "$0: $i from $(date -d @$tmp_start) to $(date -d @$tmp_stop)"

        # if recording is active, immediately stop suspend 
        # tmp_stop > now and tmp_start < now 
        if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -lt $((current_date)) ]; then
                name=`grep -h -A 1 title $i | grep -v  title | sed 's/.*: "\(.*\)"$/\1/'`
                logger "$0: Currently RECORDING $name. No Suspend until $(date -d @$tmp_stop!)"
                exit 1;

        # only check future recordings
        # tmp_stop > now and tmp_start > now 
        if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -gt $((current_date)) ]; then

                # take lower value (tmp_start or start_date)
                # (start_date = 0) or  (tmp_start < start_date)
                if [ $((start_date)) -eq 0 -o $((tmp_start)) -lt $((start_date)) ]; then
                        name=`grep -h -A 1 title $i | grep -v  title | sed 's/.*: "\(.*\)"$/\1/'`


# set up wakeup alarm
if [ $((start_date)) -ne 0 ]; then
        logger "$0: Set Wakealarm for $name to $(date -d @$wake_date)"
        echo 0 > /sys/class/rtc/rtc0/wakealarm
        echo $wake_date > /sys/class/rtc/rtc0/wakealarm

Now set up KDE to handle standby after time-out. If a recording is active, KDE standby is aborted by the above script.

See also this in the MythTV documentation.