How To Setup Automatic Shutdown and Automatic Wakeup in Ubuntu 9.10 Karmic MythTV

If you are interested in helping your power bill by a small amount, then this How To will assist you in setting up your MythTV computer to automatically shutdown between recordings and automatically wake back up using the BIOS real-time clock wakeup technique.

It took me a few days to finally get this working for me, but this may work for several other motherboards.  This was specifically tested on an ECS GF8200A motherboard.

The first step is to ensure you go into your BIOS and change a setting under the “Power Management Setup” entry.  In there, another entry which says “Resume on RTC Alarm” whould be DISABLED.  Although this seems like backward logic, this is the correct way to setup the entry to allow Ubuntu to write to the ACPI RTC “file”.

OK, now that the BIOS setting is done, it is time to boot into Linux.  One file needs to be modified just a bit.  Open the /etc/default/rcS file with a text editor (vi, gedit, mc – whichever you prefer).

sudo vi /etc/default/rcS

Now, you need to check one setting.  One says “UTC=…”  Make sure this setting says:

UTC=yes

After that, you will most likely need to add a line to the end of the file:

HWCLOCKACCESS=no

Now reboot one more time to ensure that these settings will take affect correctly.

After rebooting, it is now time to check the actual ACPI wake-up technique on your PC.

To do so, issue this one command.  This command will set the clock to turn your computer on five minutes from the time you issue the command:

sudo sh -c “echo `date ‘+%s’ -d ‘+ 5 minutes’` > /sys/class/rtc/rtc0/wakealarm”

To check to ensure the setting took, enter:

cat /sys/class/rtc/rtc0/wakealarm

It should give you a large group of numbers to indicate the alarm has been set successfully.

Now, shudown your computer (not just a restart).  Hopefully your computer will turn back on automatically.

If your  computer turns back on in five minutes, then you’ve confirmed that your BIOS will automatically turn back on – and all of your settings for this feature are correct!

Now it is time to get the capability built within MythTV.  For this, there are a few scripts that need to be created.  This first script is a simple one that will check to see if a user is logged in.  This is a custom script that I made and simply checks to see if certain programs are running.  If they are running, the computer will be prevented from being shutdown.  You will see that I’ve made entries for things like firefox, audacious, mythfrontend, boxee, checking to see if someone has an ssh connection open, and some others.  You can modify this to meet your needs.  Save it in your home folder as checklogin.sh:

# Check to see if anyone is currently logged in. Return zero if not and 1 if so.
# Echoed text appears in log file. It can be removed and –quiet added to the
# grep command once you are satisfied that mythTV is working properly

if
  w | grep “pts”
  then
     exit 1
fi
if
  ps -A | grep “boxee”
  then
    echo Someone is still logged in, don\’t shut down!
    exit 1
fi
if
  ps -A | grep “mythfrontend”
  then
    echo Someone is still logged in, don\’t shut down!
    exit 1
fi
if
  ps -A | grep “audacious”
  then
    echo Someone is still logged in, don\’t shut down!
    exit 1
fi
if
  ps -A | grep “firefox”
  then
    echo Someone is still logged in, don\’t shut down!
    exit 1
fi
if
  ps -A | grep “mythcommflag”
  then
    echo Someone is still logged in, don\’t shut down!
    exit 1
fi
    exit 0

After saving the file, ensure you set this file to be executable:

chmod +x checklogin.sh

One more script to make and we’ll be set.  This next script sets the logic that will set the ACPI wake-up time before the computer shuts down.  Save this file as MythWakeSet in your home folder:

< id=”code”>#!/bin/sh

# inspired from http://www.mythtv.org/wiki/index.php/ACPI_Wakeup
# and https://help.ubuntu.com/community/MythTV/Install/WhatNext/ACPIWake
#
# MythWakeSet
#
# set mythtv wake-up time with UTC-adjusted time
#
# use: MythWakeSet date time     
# ex.: MythWakeSet 2008-11-02 20:15:00
# See also ‘man date’ for date/time-formats.

# TimeZone, use +0100 for GMT+1
#TZ=”+0100″
TZ=$(date +%z)

LOG=/var/log/mythtv/mythbackend.log

DATE=$(date -d “$1 $2 $TZ” “+%F %H:%M:%S” -u)
SECS=$(date -d “$1 $2” “+%s”)

echo Running $0 to set the wakeup time to $1 $2 >>$LOG

if [ -e /sys/class/rtc/rtc0/wakealarm ]; then
  echo 0 > /sys/class/rtc/rtc0/wakealarm
  echo $SECS > /sys/class/rtc/rtc0/wakealarm
  echo “echo 0 > /sys/class/rtc/rtc0/wakealarm” >>$LOG
  echo “echo $SECS > /sys/class/rtc/rtc0/wakealarm” >>$LOG
  cat /proc/driver/rtc  >>$LOG
else
  if [ -e /proc/acpi/alarm ]; then
    echo $DATE > /proc/acpi/alarm
    echo “echo $DATE > /proc/acpi/alarm” >>$LOG
  else
    echo “ERROR, Wakeup not set, no /sys/class/rtc/rtc0/wakealarm and no /proc/acpi/alarm found” >>$LOG
  fi
fi</>

Do the same thing with this file as well to ensure it is executable:

chmod +x MythWakeSet

Now, one more last thing to do before getting into the settings of MythTV Backend to make this all possible.

You need to allow the mythtv user (or whatever user is logged into the computer) to run the MythWakeSet script and shutdown routines without being a root user.  To do this, type:

sudo visudo

At the end of the file, put in the following (the first word may vary depending on your username used – I put in both entries just to be safe):

%mythtv ALL = NOPASSWD: /home/user/.mythtv/MythWakeSet, /sbin/shutdown, /bin/sh
%user ALL = NOPASSWD: /home/user/.mythtv/MythWakeSet, /sbin/shutdown, /bin/sh

 Now all of the configuration for the operating system is done.  It is time to open up your MythTV Backend to make some changes.  Go to your Menu at the top (it will say “System”), choose “Administration”, and then choose “MythTV Backend Setup”.

Once it loads, go into the “General” item and then you will have to go through a few screens until you get to the Wakeup/Shutdown settings.

– Uncheck the Block Shutdown checkbox

– Idle Shutdown Timeout – set this to how many minutes you want your computer to be idle before it shuts down.  I have mine set to 10 minutes (600 seconds).  This way if all of your applications are closed on the computer (basically those that you outlined in your checklogin.sh script), the computer will shut down.

–  Max wait for recording – set this to how long the computer will stay on if a recording is scheduled.  If you have a recording that will take place in two hours and want your computer to stay on, you will put 120 in this field (since this is in minutes).  This ensures that the computer will not shut down and then turn back on x minutes later for a recording.  If you only have 30 minutes between a recording, it would be pointless to shutdown the computer for 20 minutes (30 minutes – the Idle Shutdown Timeout setting indicated above).

Startup Before Recording – this will set extra time to allow the computer to boot up.  If a recording starts at 3:00 pm and your computer takes a minute to boot, you will want to ensure this setting is set to at least 60 seconds (I would do 120 seconds to be safe – in case a disk check or something else runs).  Otherwise if you do not set this, the computer will start up right when the recording is supposed to start at 3:00 pm and cut off part of your recording.

Wakeup Time Format:  Set this to: yyyy-MM-dd hh:mm:ss

Command To Set Wakeup Time: sudo -H /home/USER/MythWakeSet “$time”     —– This command should point to the script that you created above to setup the ACPI wake-up time.  Note to change the USER part with your username for the home folder where you saved the script.

Server Halt Command:  sudo -H shutdown -P now    ———– This is the command that will shutdown your computer.  I used the -P setting which means to power off the computer for the full shutdown.

 Pre Shutdown Check Command:  sudo -H /home/USER/checklogin.sh    ———- This command should point to the script that you created above that will check to see if there are any specific programs running.  If they are, this will ensure the computer does not shutdown.  Note to change the USER part with your username for the home folder where you saved the script.

That is all!  Now close out of Myth-Backend and you should be ready to go!