Configuring MythTV Automatic Wakeup and Shutdown
Summary
This tutorial outlines the steps to configure MythTV automatic wakeup
and shutdown on a custom MythTV Personal Video Recorder originally
running Mythbuntu.
Update 2014-11-13: These steps were originally documented using
Mythbuntu 12.04.2, and have been updated to cover Mythbuntu 14.04.1.
In order to be of use to users on other hardware or MythTV versions, I
try to explain why specific choices are made.
This tutorial is one in a series of articles that started
with cutting the
cord on cable
TV, selecting
hardware for a MythTV PVR,
and installing
Mythbuntu 12.04.2 with MythTV 0.25, and more
recently installing
Mythbuntu 14.04.1 with MythTV 0.27. Note that although I am
updating this article in November 2014, we have had our MythTV PVR
reliably automatically waking up and shutting down since shortly after
we first installed MythTV in May 2011.
Contents
- Why Configure MythTV Automatic Wakeup and Shutdown
- A. Exiting MythTV Frontend after Mythbuntu Boot
- B. Opening Terminal Window
- C. Test Real Time Clock Wakeup
- D. Navigating MythTV
- E. Check MythTV Frontend Paint Engine Setting
- F. Setup Scripts
- G. Configure MythTV Backend
- H. Configure Mythwelcome
- I. Alter Mythwelcome Settings (Mythbuntu 14.04.1)
- J. Reboot Computer
- Instructions for Using Mythwelcome
- Testing [Optional]
- Automatic Shutdown/Wakeup Configuration Complete
- References
- Future Articles
Why Configure MythTV Automatic Wakeup and Shutdown?
The main reason to configure a MythTV PVR for automatic wakeup and
shutdown is to save money on your power bill. If you do not mind
leaving your PVR running all the time (24/7), then you can skip this
tutorial.
At this point we have built a stand-alone PVR with both a MythTV
backend (for recording) and frontend (for viewing). Because my spouse
and I wanted maximum power savings we chose to use the real time clock
(RTC) on the motherboard to wakeup the PVR. One inconvenience of this
method is that when the PVR is powered down then we must physically
press the power switch on the PVR and then wait for it to completely
boot up before we can start watching recorded shows.
If you wish to avoid this inconvenience then another option available
is to place the PVR in a sleep state whereby the USB ports remain
powered to receive signals from a remote control. Note that such a
scenario draws more power in the sleep state as compared to being
powered down. As such we did not pursue this option. Further, I have
read that due to problems with some hardware waking up from a sleep
state, it is advisable to have the PVR reboot immediately after waking
up from sleep.
If you have a MythTV PVR backend with multiple MythTV frontends, then
you might want to consider configuring wake on LAN. Wake on LAN is
the ability of one computer to power-up when it receives a specially
crafted network packet from another computer. For this to work, you
will need a motherboard that
supports Wake on
LAN.
To summarize my spouse and I opted for maximum power savings by
implementing only the real time clock (RTC) wakeup, and we did not
implement a minimal sleep state or wake on LAN.
A. Exiting MythTV Frontend after Mythbuntu Boot
In several of the following configuration steps we will need to reboot the computer. By default the computer will boot into the MythTV Frontend. This is perfect for when we are using the computer as a PVR, but since we have more configuration to perform we need to exit out of the MythTV Frontend.
TIP: If prompted with a warning regarding connection to the backend database, simply acknowledge this by pressing the Esc or Enter key. |
To exit the MythTV frontend:
- Press Escape key until prompted: Do you really want to exit MythTV?
- Use arrow keys to select Yes, Exit now and press Enter key.
B. Opening Terminal Window
Several of the following steps require commands to be entered in a
terminal window.
To open a terminal window:
- Start Applications -> System -> Terminal
C. Test Real Time Clock Wakeup
Before configuring automatic MythTV wakeup, we wish to confirm that the motherboard actually wakes up from a powered off state. In a terminal window enter the following commands:
- Set the Real Time Clock (RTC) to wakeup after 5
minutes.
sudo sh -c "echo 0 > /sys/class/rtc/rtc0/wakealarm"
sudo sh -c "echo `date '+%s' -d '+ 5 minutes'` > /sys/class/rtc/rtc0/wakealarm"
- Check that the RTC time has been set about 5
minutes into the future.
cat /proc/driver/rtc
In my test the contents of the file were as follows:
---------- begin contents of /proc/driver/rtc/ ----------
rtc_time : 18:18:00
rtc_date : 2014-01-17
alrm_time : 18:22:42
alrm_date : 2014-01-17
alarm_IRQ : yes
alrm_pending : no
update IRQ enabled : no
periodic IRQ enabled : no
periodic IRQ frequency : 1024
max user IRQ frequency : 64
24hr : yes
periodic_IRQ : no
update_IRQ : no
HPET_emulated : yes
BCD : yes
DST_enable : no
periodic_freq : 1024
batt_status : okay
---------- end contents of /proc/driver/rtc/ ----------
Check the "alrm_time" is about 5 minutes into the future and the "alrm_date" is today.
- Shutdown your computer and see if it comes back up
in ~5 min.
sudo shutdown -P now
After your computer shuts down, if all goes well, it should power up again within 5 minutes (assuming of course that you completed all of the above commands in less than 5 minutes.)
If your computer does not automatically wakeup at the expected date and time, then you might read ACPI Wakeup for tips on how to get this to work.
D. Navigating MythTV
In some of the following steps we need to navigate through the MythTV backend or frontend to alter values. Rather than detailing each keypress required, I have opted to describe how the keys work.
Key Action --------- ------------------------------ Tab Moves forward through options Shift+Tab Moves backward through options Esc(ape) Backs out of a screen or setting Enter Enters a screen or setting Right arrow Like Enter, but also change value Left arrow Like Esc, but also change value Down arrow Move down through options Up arrow Move up through options
E. Check MythTV Frontend Paint Engine Setting
The MythTV frontend handles the display of recordings from the
backend. Unfortunately with the MythTV 0.25 version included with
Mythbuntu 12.04.2 I have discovered that the mythbackend setup screen
does not work with the paint engine set to OpenGL.
Further complicating this situation is that mythwelcome does not work
with the paint engine set to Qt. The solution is to
ensure that the paint engine is set to Auto.
Note that Mythbuntu 14.04.1 with MythTV 0.27 does not suffer from this
problem and works well with the paint engine set
to Auto.
TIP:
Set Paint Engine to Auto Setting. |
- If the MythTV Frontend is not running then choose Applications -> Multimedia -> MythTV Frontend
- Select Setup -> Appearance
Ensure paint engine is set to Auto.On Theme / Screen Settings page: Paint engine: Auto
If you change the paint engine setting then make sure to navigate through the three Next buttons and then the Finish button so that the change is saved. You will also need to exit the MythTV Frontend using the steps described in Exiting MythTV Frontend after Mythbuntu Boot, and then reboot the computer.
To reboot:
- Start Applications -> System -> Logout
- Choose Restart.
F. Setup Scripts
In order to use mythwelcome, which is a new welcome screen introduced for automatic shutdown and wakeup, we need to enable mythwelcome. We also need to create a script to set the Real Time Clock (RTC) on shutdown, and to ensure that the mythtv user has permission to execute the script without providing a password.
In a terminal window, enter the following commands:
- Edit /etc/mythtv/session-setting to uncomment the
MYTHWELCOME line.
sudo leafpad /etc/mythtv/session-setting
Delete the hash mark and space "# " from in front of the line:
# MYTHWELCOME=true
Then save the file an exit the editor.
- Create the script /usr/bin/setwakeup.sh to set the
wakeup time.
sudo leafpad /usr/bin/setwakeup.sh
Cut and copy the script contents into the editor.
---------- begin contents of /usr/bin/setwakeup.sh ----------
#!/bin/sh
#
# set ACPI Wakeup time
# usage: setwakeup.sh seconds
# seconds - number of seconds from epoch to UTC time (time_t time format)
#
# set UTCBIOS to true if bios is using UTC time
# set UTCBIOS to false if bios is using local time
UTCBIOS=true
if $UTCBIOS
then
#utc bios - use supplied seconds
SECS=$1
else
#non utc bios - convert supplied seconds to seconds from
#epoch to local time
SECS=`date -u --date "\`date --date @$1 +%F" "%T\`" +%s`
fi
echo 0 > /sys/class/rtc/rtc0/wakealarm # clear alarm
echo $SECS > /sys/class/rtc/rtc0/wakealarm # write the waketime
---------- end contents of /usr/bin/setwakeup.sh ----------
Save the file and exit the editor.
- Make the setwakeup.sh script executable.
sudo chmod a+x /usr/bin/setwakeup.sh
- Ensure that the script /usr/bin/setwakeup.sh is in
the sudo list for the mythtv user.
Note this enables the mythtv user to set the RTC wakeup time without having to provide a password.
CAUTION: Enable ability to recover from editing mistakes.
Open another terminal window and run "sudo bash" so that you have a shell with root privileges. This way you will be able to recover if you make a mistake in /etc/sudoers.d/mythtv that causes sudo to fail. You can test with "sudo ls" after updating sudoers.
sudo leafpad /etc/sudoers.d/mythtv
Copy and paste the sudoers contents into the editor, ensuring that there is a line feed (press enter) at the end of the line. Note that all of the text should be in one line, not multiple lines.
---------- begin contents of /etc/sudoers.d/mythtv ----------
%mythtv ALL = NOPASSWD: /sbin/shutdown, /bin/sh, /usr/bin/setwakeup.sh, /usr/bin/mythshutdown, /sbin/poweroff, /sbin/reboot
---------- end contents of /etc/sudoers.d/mythtv ----------
Set the permissions for the /etc/sudoers.d/mythtv file.
sudo chmod 0440 /etc/sudoers.d/mythtv
If the above sudo chmod... command does not work then using the other terminal window you opened (as noted in the Caution above), remove the file with rm -f /etc/sudoers.d/mythtv and repeat the steps to create the file again.
TIP: sudo permission message
Until the permissions are changed, you will see the following message when using sudo:
sudo: /etc/sudoers.d/mythtv is mode 0644, should be 0440
G. Configure MythTV Backend
Before MythTV can automatically shutdown and wakeup later to record
shows, we need to update the backend configuration.
For our setup we chose to shutdown MythTV if it would be idle for more
than 5 minutes, but only if a new recording was not scheduled within
the next 5 minutes. We also allowed 5 minutes for the PVR to boot up
so that it is ready to record. These values have worked well in
practice.
- Start Applications -> System -> MythTV Backend Setup
- Click Yes to close any currently running mythbackend processes.
- Enter your "pvr" account password.
- Configure the Shutdown/Wakeup Options as described
below:
1. General On Shutdown/Wakeup Options screen Startup command : (Must be blank) Block shutdown before client connected : unchecked Idle shutdown timeout (secs) : 300 (if using active EIT this may need to be set to a lower value) Max. wait for recording (min) : 5 Startup before rec. (secs) : 300 (make this time long enough to complete the boot & disk check before the recording should start) Wakeup time format : yyyy-MM-ddThh:mm:ss (Must be this format for mythshutdown) Command to set Wakeup Time : sudo sh -c "mythshutdown --setwakeup $time" Server halt command : sudo sh -c "mythshutdown --shutdown" Pre Shutdown check-command : mythshutdown --check
Make sure to navigate through the six Next buttons and then the Finish button so that the change is saved.
- Press Esc until you exit the MythTV Backend.
- Enter your "pvr" account password to start mythv-backend.
- Click Yes to run mythfilldatabase.
H. Configure Mythwelcome
We also need to configure the mythwelcome screen for automatic shutdown and wakeup.
- From a terminal window, enter the following command:
mythwelcome --setup
- Configure the MythShutdown / MythWelcome Settings
as described below:
Command to set wakeup time : sudo sh -c "/usr/bin/setwakeup.sh $time" Wakeup time format : time_t nvram-wakeup Restart command : (Must be blank) Command to reboot : /sbin/poweroff (Yes, poweroff is correct) Command to shutdown : sudo shutdown -P now Command to run xterm : xfce4-terminal Command to start the frontend : /usr/bin/mythfrontend
Make sure to choose Finish button so that the change is saved.
I. Alter Mythwelcome Settings (Mythbuntu 14.04.1)
On Mythbuntu 14.04.1 I discovered that if Mythwelcome was set to
automatically start Mythfrontend, then this prevented automatic
shutdown until Mythfrontend was exited. This becomes a problem when
there is a power outage and power is restored because the PVR would
remain running until someone manually exited Mythfrontend.
After a short time of running Mythbuntu 14.04.1, I noticed a change in
behaviour in that upon boot-up both Mythwelcome and Mythfrontend
cannot connect to the backend database, but now retry until this is
successful. I did not observe this behaviour with Mythbuntu 12.04.2.
I believe that the connection that Mythfrontend establishes with
Mythbackend is recognized as a user of the Mythbackend and hence
prevents auto-shutdown.
To avoid this minor problem, alter the Mythwelcome settings to not
automatically start Mythfrontend. Note that this setting will likely
work fine with Mythbuntu 12.04.2 as well, though I did not test it.
- From a terminal window, enter the following command:
mythwelcome
If you are taken to Mythfrontend, then exit Mythfrontend to get back to the Mythwelcome screen.
- Press the i key to access Mythwelcome
Settings.
- Configure the MythWelcome settings as described
below:
Automatically Start mythfrontend: disable
Make sure to choose Finish button so that the change is saved.
- Press the M key to bring up a menu.
- Select option Exit to exit out of Mythwelcome.
J. Reboot Computer
To test our changes, reboot the computer.
- Start Applications -> System -> Logout
- Choose Restart.
On bootup one of two things should happen.
- If there are no scheduled recordings within the next 5 + 5 minutes, then mythwelcome should invoke mythfrontend.
- If there are scheduled recordings within the next 5 + 5 minutes, then mythwelcome will not invoke mythfrontend. In this case you can press Enter to select Start Frontend.
Instructions for Using Mythwelcome
Now that you have configured your MythTV PVR for automatic shutdown
and wakeup, there are some key concepts that are important to know.
Always Let Mythwelcome Shutdown the PVR
When finished watching MythTV recordings and you are finished using
the PVR, exit the MythTV Frontend and leave the PVR at the Mythwelcome
screen. Do not manually shutdown the system.
This is critical so that mythwelcome can set the Real Time Clock and
then shutdown the PVR. This allows the RTC to wakeup the PVR for the
next scheduled recording. If the PVR is shutdown without setting the
RTC, then the PVR will not automatically wakeup and scheduled
recordings will be missed.
Use Power Switch to Power Up PVR
If the PVR is powered off and you wish to use it then you will need to
manually press the power switch to boot up the PVR.
Since we configured the PVR to automatically shutdown in 5 minutes if
there is no active connection to the backend database, we have 5
minutes to create such a connection. This can be done by navigating
to the Media Library and displaying a list of recorded shows. If a
list of shows is not displayed then keep trying until a successful
connection is made to the backend database.
Note this opens a connection to the MythTV backend database and
will stop premature shutdown. Later when you exit the MythTV
frontend, this connection to the backend database will be terminated.
Temporarily Using PVR as a Desktop Computer
If you wish to temporarily use your PVR as a desktop computer, and not be surprised by the PVR automatically shutting down, and also not miss any recordings, then use the following instructions.
- If the PVR is not running, boot the PVR.
- [Mythbuntu 12.04 only] When mythfrontend
starts up wait ~30 seconds and then keep trying to go into the
Media Library until you get a listing of recorded shows.
Note this opens a connection to the MythTV backend database and will stop premature shutdown.
- [Mythbuntu 12.04 only] Exit out of mythfrontend, see Exiting MythTV Frontend after Mythbuntu Boot. The mythwelcome status screen should be displayed.
- Press the M key to bring up a menu.
- Select the first option, Lock shutdown.
- Press the M key to bring up a menu.
- Select option Exit to exit out of
Mythwelcome.
- Proceed to use your PVR as you would a desktop
computer.
During this time mythbackend is running so it will record any shows that are scheduled to be recorded during this time period.
- When ready to return your PVR to normal MythTV operation, start Applications -> Multimedia -> MythTV Frontend
- [Mythbuntu 12.04 only] Exit out of MythTV, see Exiting MythTV Frontend. The mythwelcome status screen should be displayed.
- Press the M key to bring up a menu.
- Select the first option, Unlock shutdown.
- Leave the PVR in this state to permit it to shutdown by itself.
Testing [Optional]
At this point your MythTV PVR should now be correctly setup to
automatically shutdown and later wakeup to perform a scheduled
recording.
In order to ensure you do not miss your favourite show, you can test
by scheduling a show to record that is at least 15+ minutes into the
future. Then leave the PVR at the Mythwelcome status screen. Watch
to confirm that the PVR shuts down in 5 minutes. Check later to see
if the PVR started up to record the show.
NOTE:
For Testing, Schedule Recording At Least 15 Minutes In the Future |
Automatic Shutdown/Wakeup Configuration Complete
Congratulations, you have now configured your MythTV PVR to automatically shutdown during idle periods, and later wakeup to capture scheduled recordings. Now you can benefit from a reduced electric power bill due to reduced power usage. :-)
References
While configuring the Mythbuntu MythTV PVR for automatic wakeup and shutdown, I found the following references useful:
For users that want to stream video content from the Internet using a MythTV PVR, see tear free Firefox menu in MythTV.
Future Articles
In future articles, I plan to cover the following steps of our path: