Automated shutdown/wakeup

News

2005-03-19: New version 0.4 is available for download. Added grub support. Added a lot of processes to check for idle time. Fixed bug with future recordings that are not currently scheduled. You need to enable Freevo's network remote control. See updated config infos above.

Intro

One of the core features of a PVR should be the functionality of an automated system wakeup to perform a scheduled recording and the shutdown afterwards. There is an open-source project called nvram-wakeup [1] which offers the wakeup part. It was originally designed for the vdr-project. Unfortunately Freevo doesn't offers native support for it.

Bash hack

Due to my limited python skills I decided to write a bash script which offers the wakeup and shutdown functionality. Of cource it would be nicer to have a native Freevo support but I needed this feature so much that I gave it a shot. See the wakeup_check-tarball on the download page. Setup shouldn't be difficult at all after you've read this page!

Now that I have the script running for a few days I'm really satisfied with it. It lacks a few features which a native solution would probably offer, but I hope to solve these issues soon. I will discuss this later, maybe I should start with the current features:

The script is designed to run on a standalone Freevo box. It checks for Freevo activity. But what actually means Freevo activity here ? As a bash script it can monitor external program calls from Freevo only. It checks if there is mplayer, mencoder, xine, tvtime, etc. running. If not, it starts counting the idle time. If the idle time exceeds a certain value, it reads the scheduled recordings from /var/cache/freevo/upsoon, checks if there is a recording that will start in less than x sec, continues or aborts otherwise, calls nvram-wakeup and performs a shutdown. The script is executed via cron and when a user shuts down Freevo manually.

As you can see there is one problem right now: The script doesn't detects the following user activities: reading mails or news, checking the weather forecast or watching pictures.

Currently there are two solutions for this problem:

1st: Use an idle time of 1/2h or whatever normally offers you enough time to perfom the non-detectable task with your Freevo box. You can always turn on the radio or music as detached player, go on with the non-detectable task and the bash-script won't bother you.

2nd: A user can start/stop the idle check with a simple lockfile. It could easily be done with the commands-plugin (erase/write a lock file). All you'd have to do to pause/resume the automated shutdown is to choose a Freevo submenu item. You will find detailed infos in one of the following paragraphs.

Setup

First of all you need a running nvram-wakeup. You should get the current version from CVS, the tarball is quite outdated and doesn't support lots of motherboards. There is a good setup info written from Hubertus Sandmann available at [2]. My script supports wakeup with/without reboot. If your hardware needs a reboot to perform a proper wakeup, you have to add a new entry to your boot manager. Both lilo and grub are supported as boot manager. While lilo is running out-of-the-box grub needs to be patched. If you're using lilo you need a valid section named "PowerOff" in lilo.conf, as described in the setup infos from [2]:
image = /boot/vmlinuz
label = PowerOff
root = /dev/hda2
initrd = /boot/initrd
append = "0"
Alternatively you can use grub as boot manager. By the time writing this doc version 0.95 is the current release. This release needs the "oneshot"-patch from [3]. Add the following section to your grub.conf or menu.lst:
title PowerOff
# set "0" to your default entry
savedefault --default=0 --once
halt
We now have completed the nvram related issues of this setup.

Let's start with the script part. Add:

ENABLE_SHUTDOWN_SYS = 1
ENABLE_NETWORK_REMOTE = 1
SHUTDOWN_SYS_CMD="/path/to/wakeup_check.sh shutdown"  
to your local_conf.py. Mind the command line parameter! Otherwise you won't be able to perfom a manual shutdown. The "Watch TV" menu item (in Freevo's main menu) has to be your first menu entry. We now have completed the Freevo related issues of this setup.

The next step would be to setup the variables in the bash script. There are mostly self-explanatory. We should take care of the boot manager related variables first:

  • $NEED_REBOOT: set it to 1 if your hardware needs a reebot to wakeup properly. You will understand this if you have allready setup nvram-wakeup.
  • $BOOTLOADER: only necessary if your hardware needs a reboot. Choose your bootloader: "lilo" oder "grub" are supported.
  • $ENTRYNR: only necessary if your using grub as bootloader. Entry nr of the poweroff-section in grub.conf/menu.lst. Be aware: counting starts at "0", so "1" would be your second entry.

Second check these vital variables - if they aren't configured properly, the wakeup-script won't work:

  • $UPSOON: the freevo file with the scheduled recordings, usually located at /var/cache/freevo/upsoon
  • $PATHTONVRAM: the path to the nvram-wakeup binary
  • $UTCADJUST: I'm afraid this value could be hard to understand. It corrects the time difference between the bios time and the current system time of your freevo box. If both times are set to UTC or to local time, you should set this variable to 0. If your bios is set to UTC time, you should set it to the amount of seconds your local time differs from UTC. For Germany, which is UTC+1h the value has to be "- 3600". For USA east coast wich is UTC-6h, the value has to be "+ 21600". If you set this variable to a value which doesn't reflects your real time difference between bios and system time your recordings will always start at a false time.
  • $SEARCHSTRING[*]/$STRING_AT: if your freevo box is running in English language there is no need to change these variables. Otherwise you have to convert these strings to your language freevo is running with. Do a "cat /var/cache/freevo/upsoon" to see what freevo outputs.
  • REMOTE_CONTROL_HOST/REMOTE_CONTROL_PORT: set these two variables to their corresponding values of the same variables in your local_conf.py.
At last check the variables which tune the wakeup and shutdown functions. Adjust to your personal needs. If you're not sure, you can leave them untouched.
  • $CHECKINTERVAL: the interval in seconds of the wakeup_check-cron job
  • $MAXIDLE: the max. idle time of the freevo box

If the idle time exceeds the value of $MAXIDLE + 1x$CHECKINTERVALL, the script will shutdown the system.

  • $RECORDPADDING: the wakeup time will take this value into account and start earlier if necessary. This variable should match the value of TV_RECORD_PADDING from your local_conf.py. A differing value wouldn't make much sense.
  • $SECUNTILSTART: min. seconds between shutdown and next scheduled recording. If the script wants to shutdown it checks this value first. If the seconds until next recording would start are less than this value, no shutdown will be performed.

As I have stated before the bash script lacks some features a native solution would offer. You can build your own tiny freevo integration with the commands plugin. Activate the plugin with:

plugin.activate('command', level=XX)
COMMANDS_DIR = '/path/to/fxd-files'
Now copy these two fxd-files (shipped with the wakeup_check-tarball on my download page) into the COMMANDS_DIR directory:
<?xml version="1.0" ?>
<freevo>
  <command title="Pause automatic shutdown">
    <cmd>touch /path/to/wakeup_lock</cmd>
  </command>
</freevo>

<?xml version="1.0" ?>
<freevo>
  <command title="Resume automatic shutdown">
    <cmd>rm -f /path/to/wakeup_lock</cmd>
  </command>
</freevo>
The path and file stated in the <cmd>-section has to be the same as in:

  • $CHECKLOCK: full path and filename of the lockfile. If it is present, the automatic shutdown will pause. The file will be removed on every system shutdown.

You can now control the shutdown/wakeup-feature from within freevo. It's not very stylishly, but it works... Last step would be to add a cronjob like:

*/10 * * * * /path/to/wakeup_check.sh
This runs the script every 10 minutes. You can add this cronjob as root with:
crontab -e

That's it. Have fun!

Links

[1] http://nvram-wakeup.sourceforge.net
[2] http://home.t-online.de/home/hubertus.sandmann/vdr_wakeup.htm
[3] http://www.linuxfromscratch.org/patches/downloads/grub/

  freevo.sr-club.de