HowTo wakeup XBMC/TVHeadend for scheduled recording.

1. Make sure you motherboard supports ACPI Wakeup
Look at the nice MythTV-Wiki page: http://www.mythtv.org/wiki/ACPI_Wakeup for detailed description.

2. Make sure you have correctly working suspend script. In my case it was important to suspend system
- not to shutdown it. Please check suspend state your system (BIOS) is able to wakeup from:

$ grep -i rtc /var/log/kern.log
RTC can wake from S4
...
rtc0: alarms up to one month

3. Check if all your hardware drivers support suspend (means can easly wakeup from suspend).
If some of them not, you should remove them before suspend and load them again after wakeup.
Here is example I use (/etc/pm/sleep.d/99_htpc.sh):

#!/bin/sh

# This script uses curl. Install curl using the following command from your terminal apt-get install curl
# This script will restart lirc drivers, Lirc, and XBMC's lirc interperater upon resume.

case "$1" in
suspend|hibernate)
service xbmc-live stop
/etc/init.d/tvheadend stop
/etc/init.d/lirc stop
rmmod mantis
rmmod ivtv
rmmod nvidia
;;
resume|thaw)
modprobe nvidia
modprobe mantis
modprobe ivtv
sleep 1
/etc/init.d/lirc start
/etc/init.d/tvheadend start
#remove the comment if the computer automatically sleeps after resume
#irw & sleep 1; killall irw
sleep 2
service xbmc-live start
;;
esac

4. Create new script (the same path i.e. /etc/pm/sleep.d/95_timer.sh) to check if there is
new upcoming recording event. Script checks also the earliest event's date. Modify "safe_margin"
variable to be sure system will be ready before recording will start.

#!/bin/bash
#
# set ACPI Wakeup alarm
# safe_margin - minutes to start up system before the earliest timer
# script does not check if recording is in progress
#
#

echo 1 > /timer

# bootup system 60 sec. before timer
safe_margin=60

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

######################

start_date=0
stop_date=0

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 ","`

# check for outdated timer
if [ $((tmp_stop)) -gt $((current_date)) -a $((tmp_start)) -gt $((current_date)) ]; then

# take lower value (tmp_start or start_date)
if [ $((start_date)) -eq 0 -o $((tmp_start)) -lt $((start_date)) ]; then
start_date=$tmp_start
stop_date=$tmp_stop
fi
fi
done

wake_date=$((start_date-safe_margin))

echo $start_date >> /timer
echo $wake_date >> /timer

# set up waleup alarm
if [ $((start_date)) -ne 0 ]; then
echo 2 >> /timer
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo $wake_date > /sys/class/rtc/rtc0/wakealarm
fi

Shutdown (suspend) system after recording

To shutdown (suspend) system after recording you can do a quick trick:

1. Check what system user is your tvheadend run on. In my case: hts
2. Create a simple script somewhere i.e. /home/hts/shutdown.sh
as follows (remember to make it executable: chmod u+x):

#!/bin/sh

# Here you can invoke your script for recording's post-processing
#exec <your_script.sh>

sleep 20
sudo /usr/sbin/pm-suspend &

3. Make sure your system user tvheadend is running on have enough priviligdes to suspend system:
add following line to /etc/sudoers (repleace 'hts' with user your tvheadend is run on):

hts ALL=NOPASSWD: /usr/sbin/pm-suspend # TVHeadend

4. Through the web page of tvheadend: Configuration -> Digital Video Recorder
point parameter 'Post-processor command' to your script i.e. /home/hts/shutdown.sh