Using ACPI & MythTV to run other applications

From MythTV Official Wiki
Revision as of 07:47, 4 January 2010 by DAP (talk | contribs) (Created page with '=== Introduction === I don't yet know how to do this, and this page is therefore not complete, but I believe it is possible. My goal is to set up my system to wake the computer …')

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Introduction

I don't yet know how to do this, and this page is therefore not complete, but I believe it is possible.

My goal is to set up my system to wake the computer and run a backup program at a time that I am unlikely to be using the computer myself, and mythTV is unlikely to be recording anything.

I'd like to use cron to schedule the external event and have the following scripts extract the relevant info from cron to determine when to power on or off the PC.

My starting point will be the desktop ACPI wakeup setup so I will copy that here so it can be modified:

ACPI setup for a desktop system

mythtv-setup settings for your script

Block shutdown: (checked, if you run frontend and backend on 1 machine. Do not check if you do not automatically start the front end, and want the system to shut down after an automatic recording.)

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

Max. wait for recording (min): 120

Startup before rec. (secs): 900 (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. On my system the disk check can take as long as 15 minutes)

Wakeup time format: time_t

Command to set Wakeup Time: sudo sh -c "/usr/bin/setwakeup.sh $time"

Server halt command: sudo shutdown -h now

Pre Shutdown check-command: checklogin.sh

Write the startup script

This needs to be modified to obtain the time of the next external (to MythTV) event and change the wake time ($1) if it is less than MythTVs wake time.

Script.png 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

sudo chmod +x /usr/bin/setwakeup.sh

Add the following line to your /etc/sudoers by running "sudo visudo" and pasting the line at the end of the file.

%mythtv ALL = NOPASSWD: /sbin/shutdown, /bin/sh, /usr/bin/setwakeup.sh
Write the pre-shutdown script

Create the following script to prevent myth from shutting down when someone is logged in.

This needs to be modified to check if an external event is pending in less than "max wait for recording" seconds, it also needs to check if an external event is running to prevent inappropriate shutdowns.

Script.png /usr/bin/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 
  # The leading space is important!
  w | grep " 0 users"
  then
    echo Noone is logged in, ok to shut down.
    exit 0
  else
    echo Someone is still logged in, don\'t shut down! 
    exit 1
fi

Change its attributes to executable

sudo chmod +x /usr/bin/checklogin.sh

You must remember to log out rather than shut down when you are done with your session, allowing the mythTV backend to shut off the computer when it is idle. This is because the mythTV shutdown sequence writes the wakealarm time to the BIOS just before shutting down the system. If, instead, you shut the system down, the mythTV backend is not able to update the BIOS with the correct wakealarm time. Failure to follow this procedure could interrupt a recording, or prevent the system from powering on for a later recording.

Note that the Idle Timeout time is the time you have to type your username and password when logging in. It would be a good idea to make it long enough to log in before the system can shut itself off.

In the BIOS, set your system to power on when power is restored. This will allow the system to recover after a power outage. Some systems will disable the wakealarm after a power outage, but even if it doesn't, if the power is off at the programmed wake time, the wakealarm could be missed. Powering up after a power outage allows mythTV to reset the wakealarm so that future recordings are not missed.

Things that still need to be resolved

I am assuming that I will use cron to initiate the external event, and the MythTV ACPI stuff to ensure that the computer is on at the time the external event needs to start. I'd like to make this setup generalized so that one can just add something to cron and have the computer power up at the correct time to execute the event. This means there must be some means of asking cron what the next events are, and somehow flagging the ones that need to have the computer turned on. Also once an external event is initiated, I must be able to tell when external event is completed so that the system knows when it is safe to shut itself down.

Editorial content

It is my belief that the ACPI power control should be separated from MythTV, and incorporated into cron. This would allow people who don't use mythTV schedule stuff like automated backups & running sprinkler systems without having to leave their computers on all the time. Implementing this change would be non-trivial, so it does not appear that this is likely to happen, and I don't yet have the skill to do this myself. I might have enough skill to make the above scripting stuff work, so I'm going to attempt it.