Sleep timer

From MythTV Official Wiki
Jump to: navigation, search

MythTV 0.25 added a sleep timer in the frontend that will automatically close out of the current operation and drop back to the main menu. The purpose of this is to unblock the shutdown mechanisms in the master backend, and let it automatically standby or shutdown as it sees fit, if configured to do so. Dedicated frontends are free to start and stop as they please, and so do not take part in this shutdown mechanism. This page and script describes how to implement an external sleep timer controllable by remote that will fulfill this purpose on dedicated frontends.

What result will you get

This approach will allow you to push a button on your remote (even when you are watching a recording) to set a sleep timer. Pushing it again will add a configured increment to your timer.

If you exceed a maximum time value, the sleep timer will be cancelled. Everytime you push the button you will get informed (by mythutil --message) about the current status of your sleep timer. I added a second small script which will warn you about one minute before the mythbox is actually shutting down. So you could still cancel the countdown and set up a new sleep time.

Sleep by remote key

Make your mythtv user able to shut down your system

...and allow him to kill a running shutdown process (which is needed to cancel actual shutdown and set it to a new value).

Execute visudo command (you will have to be root to do this):

# visudo

Your sudoers file will show up in your preferred editor. Just add a line for your mythtv user (in my case it's reznor) who will be allowed to shutdown the mythbox and kill the shutdown process:

reznor  ALL=(ALL) NOPASSWD: /sbin/shutdown,/usr/bin/pkill -f shutdown

Save and quit your editor.

sleeptimer_irexec.sh

Save this script in your mythtv users' user home. In my case I saved it as

~/bin/sleeptimer_irexec.sh
#!/bin/bash

# maximum allowed sleeptime in minutes
# if a keypress would make the sleeptimer exceed this value
# the shutdown gets cancelled
MAXSLEEPTIME=125

# the increment of the sleep timer in minutes
# when the sleep timer key is pressed
SLEEPINCREMENT=25

#######################################################

# determine script's path
SCRIPTPATH=$(dirname $(which $0))

# look for a former invocation of shutdown and save old value
OLDSLEEPTIME=$(ps auxw | grep shutdown | grep -v grep | head -n 1 | cut -f2 -d'+')
[ -z "${OLDSLEEPTIME}" ] && OLDSLEEPTIME=0
NEWSLEEPTIME=$(($OLDSLEEPTIME + $SLEEPINCREMENT))

# kill former instances of shutdown and sleepwarning
sudo /usr/bin/pkill -f shutdown >/dev/null 2>&1
/usr/bin/pkill -f sleepwarning.sh >/dev/null 2>&1

# set new sleepvalue or cancel sleep timer if it exceeds MAXSLEEPTIME
if [ $NEWSLEEPTIME -gt $MAXSLEEPTIME ]
then
        /usr/bin/mythutil --message --message_text="Sleep timer off." --timeout=3 --bcastaddr 127.0.0.1 &
        exit 0
fi

# start shutdown
sudo /sbin/shutdown -h +${NEWSLEEPTIME} >/dev/null 2>&1 &

# show message to user, that sleeptime was initiated
/usr/bin/mythutil --message --message_text="$(hostname) will go to sleep in $NEWSLEEPTIME minutes." --timeout=3 --bcastaddr 127.0.0.1 &

# Warn ~ one minute before we really go to sleep
$SCRIPTPATH/sleepwarning.sh $NEWSLEEPTIME &

As you can see in the comments you may fit the variable MAXSLEEPTIME and SLEEPINCREMENT to your needs.

sleepwarning.sh

You will also need another script which warns you one minute before the actual shutdown. This is handy if you would like to extend or cancel your sleep timer. Please put this second script into the same path as the first.

~/bin/sleeptimer_irexec.sh
#!/bin/bash

SLEEPTIME=$1

sleep $(($SLEEPTIME * 60 - 60 ))
/usr/bin/mythutil --message --message_text="$(hostname) will go to sleep in about one minute. " --timeout=55 --bcastaddr 127.0.0.1

irexec

Configure irexec

I'm on gentoo, so you might adjust the next steps for your linux distribution. Configure your init script to execute irexec as your mythtv user (in my case reznor again).

Edit
/etc/conf.d/irexec
# Options to pass to the irexec process
IREXEC_OPTS="/etc/lirc/lircrc"

# User to execute irexec as.
# Warning: Running irexec as root can open security holes
IREXEC_USER="reznor"

# Use this to disable the warning printed when starting irexec as root
# IREXEC_DISABLE_ROOT_WARNING=yes

Edit global lircrc

Now you need a key on your remote, which will setup your sleeptimer. I chose "Subtitles" / "KEY_SUBTITLE" for this.

Edit
/etc/lirc/lircrc
begin
     prog = irexec
     button = KEY_SUBTITLE
     config = /home/reznor/bin/sleeptimer_irexec.sh
end

Please adjust the path to where you saved sleeptimer_irexec.sh.

Start irexec

Start irexec by excuting the following commands as root. The second command adds irexec to your default runlevel so it gets started every time you start up your mythbox.

# /etc/init.d/irexec start
# rc-update add irexec default

Done

You may now start your sleep timer with your remote on your mythbox :)