Introduction

Most users tend to leave their mythTV system running 24 by 7. This page describes the options you have in Linux to
shutdown and automatically wakeup your machine before a recording. A glance at your last electricity bill
shows you just how expensive electricity is. You can save energy and money. [read more]

Your options

Hardware time switch - a cheap power switch and a simple cronjob to shutdown the machine can do magic.

Use the BIOS - most machines have an option in the BIOS for time-controlled booting

Use the BIOS

MythTV has a "standard option" to write the date/time of the next recording to the bios. The allows your machine to automatically shutdown (or suspend) and turn on again a few minutes before it needs to record again.
The Advanced Configuration and Power Interface (ACPI) specification is an open industry standard first released in December 1996 developed by HP, Intel, Microsoft, Phoenix, and Toshiba that defines common interfaces for hardware recognition, motherboard and device configuration and power management.

The ACPI Real Time Clock Alarm Function can be used on your mythTV system to wake-up your machine at a certain time and/or date.

If you don't have an ACPI-compliant BIOS, you could use nvram-wakeup instead,
which is a small program which modifies the BIOS settings memory (NVRAM) directly. (not recommended)
All modern BIOSes should support ACPI, however.

BIOS setup

If you want to use ACPI to wake up your mythTV system, you first need to ensure that your
BIOS has support to wake. Check your BIOS for the alarm function.
This setting is likely found under a sub-menu of "Power Management" "APM", or "Advanced" - you just need to poke around to find it. It will likely be called something like Wake from RTC, Wake From Alarm, RTC Resume, etc.

Note: There are a lot of fussy BIOSes out there. In some cases you need to disable the RTC alarm function is the bios to make things work.
After you have set the RTC alarm from Linux, it could be that you not see changes in the BIOS but it still works. This is what we call Fussy Bios.

UTC, localtime and BIOS date format

If supported by your BIOS, set the time to UTC (Coordinated Universal Time). If not supported in the BIOS you need to recalculate the time before you write it to the BIOS. (one hour difference in most cases)

Also check the format of the date and time are stored in the bios.
Most likely the format is YYYY-MM-DD HH:MM:SS

Write down these values, can be useful later ;-)

Prepare your Linux distribution

The ACPI interface in Linux has changes with kernel 2.6.22. Make sure you understand what kernel version you
are using:

2. If you want to change the wakealarm time, you first need to reset the time.

If you can't find /sys/class/rtc/rtc0/wakealarm, and you're running a modular kernel, it might be the case that the correct module is not loaded. In that case, make sure the rtc module is unloaded, and then load the rtc-cmos module:

Disable hwclock updates

On most machines it's required to make a small change to the Linux shutdown procedure.
When your machine goes down, most linux distributions write the system time/data back to the bios.
On MANY machines the machines never wakes-up after a time/data update. It's recommended to make this change
before you start. See below for more details (distro specific),

Most linux distributions write the current system time back to the bios when shutting down the machine. Some BIOSes refuse to wake up if the hardware clock is modified after the alarm timer has been set. To avoid that the current system time is written back to the hardware clock it required to change your startup scripts. This is distribution specific, so here are some examples:

Gentoo

Set clock_systohc to "NO" in /etc/conf.d/hwclock:

/etc/conf.d/hwclock

# Set CLOCK to "UTC" if your system clock is set to UTC (also known as
# Greenwich Mean Time). If your clock is set to the local time, then
# set CLOCK to "local". Note that if you dual boot with Windows, then
# you should set it to "local".
clock="UTC"
# If you want to set the Hardware Clock to the current System Time
# during shutdown, then say "YES" here.
# You normally don't need to do this if you run a ntp daemon.
clock_systohc="NO"
# If you wish to pass any other arguments to hwclock during bootup,
# you may do so here. Alpha users may wish to use --arc or --srm here.
clock_args=""

openSUSE

modifying /etc/sysconfig/clock holds a switch to write back system time to the hardware clock

/etc/sysconfig/clock

# Set to "-u" if your system clock is set to UTC, and to "--localtime"
# if your clock runs that way.
#
HWCLOCK="-u"
SYSTOHC="no"
# Is set to "yes" write back the system time to the hardware
# clock at reboot or shutdown. Usefull if hardware clock is
# much more inaccurate than system clock. Set to "no" if
# system time does it wrong due e.g. missed timer interrupts.
# If set to "no" the hardware clock adjust feature is also
# skipped because it is rather useless without writing back
# the system time to the hardware clock.

Debian (and Ubuntu)

modifying /etc/init.d/hwclock.sh with the following will fix this problem:

Integrate into mythTV

Remember, your bios time might be in UTC rather than local time using:

# echo "+00-00-00 00:05:00" > /proc/acpi/alarm

will set the wakeup time to 5 minute from now, regardless of whether the RTC is in UTC or locatltime.

Since the wake-up time given by mythtv to the mythtv wakeup command is in local time, you need to do some bash handling of the obtained time to be able to setup the wakeup time in the bios in UTC (independent of daylight savings time and such). The following bash code might help here:

As you noticed the echo to the acpi-alarm is commented out by default, because it is really better to check first if everything is working correctly (time format the same on your bios and such).

An alternative script to the above is the following:

#!/bin/bash
stamp_file=/home/mythtv/timestamp
#just log what we get as command line parameters
echo $1 $2 $3> $stamp_file
#I set mythtv to output the number of seconds since epoch
#so I calculate the number of hours, minutes and seconds from
#now the computer has to wakeup:
sfn=$(($2 - `date +"%s"`))
# Offset from GMT as the awk function does timezone correction.
tzone=3600
#and then send it to /proc/acpi/wakeup in the format we saw above
y=`(echo $(($sfn - $tzone))|awk '{print strftime("+00-00-00 %H:%M:%S", $1)}')`
echo "$y">/proc/acpi/alarm
echo "$y">>$stamp_file
echo "executed at `date`" >> $stamp_file
exit

This works on a lot of different EPIA and ASUS Motherboards, and is not dependent on the timezone you use in the bios.

Using /sys/class/rtc/rtc0/wakealarm

Beginning with kernel 2.6.22 /proc/acpi/alarm has been removed and replaced with /sys/class/rtc/rtc0/wakealarm. The key differences with the wakealarm interface are:

Instead of accepting a formatted time, wakealarm accepts the number of seconds since Jan 1, 1970.

If you want to change the wakealarm time, you first need to reset the time.

You have to make sure that your bios clock is set to UTC time not localtime otherwise it will wakeup at the wrong time

Integrate into mythTV

Block shutdown: (checked, if you run frontend and backend on 1 machine)

Idle Timeout (seconds): 1200 (if you set this to 0, it will disable auto shutdown)

Max wait (minutes): 120

Startup before rec. (seconds): 600

Wakeup time format: time_t

Set wakeuptime command: sudo /usr/bin/setwakeup.sh $time

Server halt command: sudo mythshutdown --shutdown

Pre-shutdown command: sudo mythshutdown --check

Write the startup script

You'll need to copy/paste the following into your editor:

setwakeup.sh

#!/bin/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

Change the permissions of the file so that it can execute

chmod +x /usr/bin/setwakeup.sh

If required add the script to your /etc/sudoers

Another contributed script

This acpi-alarm script will function with the new sys wakealarm interface or the old acpi/alarm interface

If leaving the RTC wakeup alarm on the BIOS worked with the old interface, you may need to disable it when migrating to the new interface.

Warning: The wakealarm interface is incompatible with the kernel's old "Enhanced Real Time Clock Support" and "Generic /dev/rtc emulation" options. If your kernel was built with these enabled your kernel log will contain messages such as

rtc_cmos: probe of 00:03 failed with error -16

The solution is to rebuild your kernel with the above two options excluded (find them under Drivers -> Character Devices) and the various RTC interfaces (found under Drivers -> Real Time Clock) included. From a .config point of view CONFIG_RTC and CONFIG_GEN_RTC must be unset and, at a minimum, RTC_INTF_SYSFS must be set.

Troubleshooting

Getting autostart using alarm can be quite frustrating. Here's some tips on getting it working.

Check your hardware

Check if your BIOS supports ACPI/RTC
You will need to have a motherboard that supports both ACPI and a Real Time Clock Alarm Functions in the BIOS. Check your BIOS and look for a tab similar to "Advanced Power management".

The wake-up function is provided on most modern boards by the ability to "wake from RTC alarm" - setting a date and time in BIOS will cause the computer to power itself on and boot at the set time. This setting is likely found under a sub-menu of "Power Management" "APM", or "Advanced" - you just need to poke around to find it. It will likely be called something like Wake from RTC, Wake From Alarm, RTC Resume, etc.

Initiate manually

see the initiate manual section

Fussy BIOS

There are a lot of fussy BIOSes out there. If things are still not working for you...try the following:

1. Disable/Enable RTC in BIOS

In some cases you need to disable the RTC alarm function is the bios to make things work.

Another possible glitch is, that the option 'Resume By Alarm' (or whatever it is called) is set to Enabled but wake up using /proc/acpi/alarm only works if the option is set to Disabled. Sounds weird but works with some boards.

On many boards, when the RTC setting is enabled in the BIOS, it will wake only from a time set and saved from BIOS setup, and not from a time set outside of the BIOS setup environment - as we want. All of the boards the original author of this document needed this setting disabled to correctly wake with ACPI. This is the recommended starting point.

2. Time/Date not visible in BIOS

After you have set the RTC alarm from Linux, it could be that you not see changes in the BIOS but it still works.

3. Required to write time/date two times

Some users reported that their BIOS is only updated after writing 2 times to the alarm file. (reported with proc/acpi/alarm)

Advanced

Compile your own kernel

For the ones that compile their own kernel, make sure ACPI is turned on. (Not required for standard distributions)
The setup of /proc/acpi/alarm is done in kernel file drivers/acpi/sleep/proc.c and controlled by the CONFIG_ACPI_SLEEP_PROC_FS flag.
You can test support for you architecture by going to you kernel source and do:

Mattwire - September 2007: Works fine with MSI K8NGM2-FID and NC6120 laptop running Ubuntu Feisty and Gutsy. Both require modification to /etc/init.d/hwclock.sh to save the wakeup time.

Dumdideldum - September 2007: Works with Epox 8rda+ running Ubuntu Feisty. It is important to disable the wakeup on rtc in the Bios and editing the /etc/init.d/hwclock.sh as described in the Ubuntu guide linked in the section below.

Pkendall - September 2007: Works fine with MSI K9NBPM2-FID running Ubuntu Feisty. As above, edit the hwclock.sh file and make sure Bios RTC wakeup is disabled.

Thewizzard - October 2007: Works with MSI K7N2 Delta-ILSR, be sure to use the hwclock.sh script and have it set the time two times.

Spitzbub - December 2007: Works with Biostar NF325-A7, be sure to modify your /etc/init.d/hwclock.sh script (or equivalent) to set the wake-up time again after the --systohc, as described above under Fussy Bios.

Per Olofsson - December 2007: Works very well with Asus P5GC-VM motherboard and Debian lenny with Linux 2.6.22. I don't use the "fussy BIOS" hack since I only suspend the system instead of shutting it down.

imcecil - April 2008: Work for Asus P5n-E SLI Gentoo 2.6.24 configured the Kernel as required above but used time_t as the wakeup time format (time since epoch) which seems to work when echoed straight to /sys/class/rtc/rtc0/wakealarm.

Cymen - April 2008: Works great for PCChips P53G but be sure to only have the rtc-cmos module loaded (not the rtc module -- if rtc module is loaded, unload both rtc and rtc-cmos and then load rtc-cmos again and the device will appear in /sys...). Blacklist the rtc module or don't compile it in (the option in under "character drivers" is the one to be excluded).