Using ACPI & MythTV to run other applications
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
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.
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.
To distinguish between cron tasks that the computer should power on for, I'm thinking of defining a user. Cron has a crontab file for each user. Within that file there would be no way to distinguish tasks that need to have the computer powered on from those that don't, so by defining a user whose sole purpose is owning these tasks, I can use that username to identify which tasks I need to pay attention to.
I can see that implementing this is going to be messy since I will have to duplicate the parsing that cron does so I can identify the next task and when it needs to be executed. Also I'm not sure how to know when a task completes. The simple idea is to see if any tasks owned by this single purpose user are running, but some tasks, like a system backup must run as root.
I took a peek at the source code to cron. Seems rather inefficient to poll once a minute when the default ubuntu setup (and probably most other distributions) use cron only for once weekly or monthly events.
I'm beginning to believe I need to do a from scratch scheduler. There are three types of events it must handle:
1: normal scheduled events like cron does now.
2: scheduled events that need to guarantee the computer is running. The scheduler needs to keep track of these tasks once started to make sure it does not shut down the system until they are complete.
3: daemons that run all the time, but need to guarantee the computer is on at a particular time. Not yet sure how to handle these to know when it is safe to shut the system down.
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.