Lester Burnham's Blog

Mythbuntu 14.04 ACPI Wake

I recently began trialing some mythbuntu 14.04 systems to wake using ACPI Wake.  I came across issues using my old methods from my previous post here ACPI Wake Mythbuntu 8.10

I use my system in the following way. (combined FE/BE & remote FE)

  • I use the mythbuntu theme
  • I do not autostart the frontend. (I remove the mythtv.desktop file from ~/.config/autostart/
  • I add XBMC to the mythtv menu and save the mainmenu.xml to ~/.mythtv/themes/defaultmenu/ (doesn’t get over written by updates)
  • I map a remote button to launch mythfrontend using irexec
  • I exit the frontend when finished, to let the backend set next wakeup time and shut itself down.

I could wake up and shutdown setting the time manually. (in 5mins)

sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm" sudo sh -c "echo `date '+%s' -d '+ 5 minutes'` > /sys/class/rtc/rtc0/wakealarm" cat /sys/class/rtc/rtc0/wakealarm
cat /proc/driver/rtc
sudo shutdown -h now

I am using UTC time.

Save the following to /etc/init/hwclock-save.conf

sudo nano /etc/init/hwclock-save.conf
# hwclock-save - save system clock to hardware clock
# hwclock-save - save system clock to hardware clock
# This task saves the time from the system clock back to the hardware
# clock on shutdown.

description	"save system clock to hardware clock"

start on runlevel [06]


    . /etc/default/rcS
    [ "$UTC" = "yes" ] && tz="--utc" || tz="--localtime"
    [ "$BADYEAR" = "yes" ] && badyear="--badyear"
    ACPITIME=`cat /proc/acpi/alarm`
    exec hwclock --rtc=/dev/rtc0 --systohc $tz --noadjfile $badyear
    echo "$ACPITIME" > /proc/acpi/alarm
end script

The next file to create is /usr/bin/setwakeup.sh  (I hope to remove the section LOG_FILE below after everything works, as I don’t want the log file growing forever)

UPDATE: I have removed everything below “echo $1 > /sys/class/rtc/rtc0/wakealarm”

sudo nano /usr/bin/setwakeup.sh
#$1 is the first argument to the script. It is the time in seconds since 1970
#this is defined in mythtv-setup with the time_t argument

echo 0 > /sys/class/rtc/rtc0/wakealarm      #this clears your alarm.
echo $1 > /sys/class/rtc/rtc0/wakealarm     #this writes your alarm

LOG_FILE='/var/log/mythtv/hwclock-rebootTime.log' #log file

# Now write the time the system is expected to come out of power save mode
# so there is at least a small record of when if it was supposed to recover

# Note:- Log file will just keep growing

# date in Epoch format
a="`date  +%s`"

# Subtract Current time from Future time
let "b=$1-$a"

# echo $b
# echo "result of time subtraction `date -d @$b`"

# Get Date and Subtract 1,.. as date starts from 1st Jan 1970
dte=`date -d @$b +%d`
let "dte -= 1"

echo "Current Time      ->`date`" >> $LOG_FILE

# Simple check to determine if to include days in output string
if (dte=0)
    echo "Shutting down for ->`date -d @$b +%Hhrs:%MMins`" >> $LOG_FILE
    echo "Shutting down for ->$[dte]Days `date -d @$b +%Hhrs:%MMins`" >> $LOG_FILE

echo "Wake up at approx.->`date -d @$1`"  >> $LOG_FILE
echo "------------------------------------------------------" >> $LOG_FILE

Set execute permissions

chmod +x /usr/bin/setwakeup.sh

Now to setup the sudoers file. I hate vi as an editor, so I use this command below.

export EDITOR=nano && sudo -E visudo

Create a section below admin%

# mythtv group
%mythtv ALL = NOPASSWD: /sbin/shutdown, /usr/bin/setwakeup.sh, /usr/bin/MythtvShutdownCheck

To exit and save file. ctrl +x, then y for yes, enter.

Now to setup mythtv for shutting down.

(use these instructions for a dedicated backend system)

Select the backend’s “General” options and on the “Shutdown/Wakeup Options” page, make the following settings:

  • Block shutdown before client connected: (un-checked. I leave this un-checked, because if the machine wakes to record, I want it to go back to sleep after. It will not, if it is waiting for the frontend to connect)
  • Idle shutdown timeout (secs): 200 (if you set this to 0, it will disable auto shutdown)
  • Max. wait for recording (min): 15
  • Startup before rec. (secs): 600 (If you have not disabled the occasional disk check on boot, make this time long enough to complete the boot & disk check before the recording should start)
  • Wakeup time format: time_t
  • Command to set Wakeup Time: sudo /usr/bin/setwakeup.sh $time
  • Server halt command: sudo shutdown -P now
  • Pre Shutdown check-command: /usr/bin/MythtvShutdownCheck

I also use another file called MythtvShutdownCheck, to block shutdown if certain conditions are met. These being if a user is logged in using SSH, XBMC, samba, chromium, grsync etc.

I have traced my current problems back to this file blocking shutdown. I’ll list it’s contents below and update it when it’s sorted.

UPDATE: The code below is now fine. I missed a couple of hashes. The easiest way to test , is to run the script the way mythtv would. “sudo sh /usr/bin/MythtvShutdownCheck” should return “Preshutdown authorized”

# checks to see if any other user/program is
# logged in or running before idle shutdown
# returns "1" if yes, stopping shutdown
# returns "0" if ok to shutdown

mdy="`date +%Y-%m-%d`"
hm1="`date +%T`"
hm2="`date +%N | cut --bytes='1 2 3'`"

echo "$mdy $hms Preshutdown"

# Check for transcoding
ps_transcode=`ps cax | grep -c mythtranscode`

if [ $ps_transcode != 0 ]; then
 echo "$mdy $hms Transcoding in progress..."
 exit 1

# Check for filldatebase
ps_filldatabase=`ps cax | grep -c mythfilldatabase`

if [ $ps_filldatabase != 0 ]; then
 echo "$mdy $hms Filling the database in progress..."
 exit 1

# Check for Commercial Flagging
ps_commflag=`ps cax | grep -c mythcommflag`

if [ $ps_commflag != 0 ]; then
 echo "$mdy $hms Commercial Flagging in progress..."
 exit 1

# optional stuff...uncomment this if you want
# Check for an SaMBa connection from another box
smb_status=`smbstatus -v | grep -c xbmc`

 if [ $smb_status != 0 ]; then
 echo "$mdy $hms Samba share still active..."
 exit 1
# Check for SuperUser connected thru BASH shell
# root_sudo=`ps cax | grep -c su`
# if [ $root_sudo -gt 1 ]; then
# echo "$mdy $hms SuperUser still logged in, recycle..."
# exit 1
# fi
# Check for DVD Burning
# ps_dvdburn=`ps cax | grep -c mythtvburn.sh`
# if [ $ps_dvdburn != 0 ]; then
# echo "$mdy $hms DVD Burning in progress..."
# exit 1
# fi

# Check for rsync
ps_rsync=`ps cax | grep -c rsync`

if [ $ps_rsync != 0 ]; then
 echo "$mdy $hms Grsync in progress..."
 exit 1

# Check for Sbackup
#ps_sbackupd=`ps cax | grep -c sbackupd`
#if [ $ps_sbackupd != 0 ]; then
# echo "$mdy $hms Sbackup in progress..."
# exit 1

# Check for Mplayer
#ps_mplayer=`ps cax | grep -c mplayer`
#if [ $ps_mplayer != 0 ]; then
# echo "$mdy $hms Mplayer is Running..."
# exit 1

# Check for XBMC
ps_xbmc=`ps cax | grep -c xbmc`

if [ $ps_xbmc != 0 ]; then
 echo "$mdy $hms XBMC is Running..."
 exit 1

# Check for Chromium
ps_chromium=`ps cax | grep -c chromium`

if [ $ps_chromium != 0 ]; then
 echo "$mdy $hms Chromium is Running..."
 exit 1

#if last | head | grep -q "pts/.*still logged in" # check for active *remote* login?
# exit 1
# exit 0

echo "$mdy $hms Preshutdown authorized"
exit 0
# End of /usr/bin/MythtvShutdownCheck

We’ll see how it goes now. Thanks to all the mythtv devs that make the best PVR around possible! Most of this info used was from the mythtv ACPI Wakeup wiki.  It only becomes a trial and error situation when a couple of different versions of files appear to set wake up times etc. So once this is all sorted, I should be good for another couple of years 🙂


Leave a Reply