[mdlug] Sleep and wake up scripts to extend MythTV Backend functionality

R Kannan rk111810 at gmail.com
Sat Apr 5 10:15:48 EDT 2014


Hi,

I am running MythTV backend (Mythbuntu 12.04) on my desktop. MythTV uses
ACPI (http://www.mythtv.org/wiki/ACPI_Wakeup) to put the system to sleep
if there are no imminent recordings and wakes the system up in time to
record scheduled shows. MythTV provides two 'hooks' for this purpose,
one command to set wake up time and another to check if the system can
go to sleep.  I had two scripts that are called for these two purposes.
The second script checks if there is a '/tmp/StopSleep' file or if there
are users logged in. In those cases, it returns non-zero value.

I thought I will extend this functionality to do other scheduled actions
(like fetch stock prices every day, create a backup image of system
every two weeks etc.) These actions are done through cronjobs run at the
scheduled times but the system needs to be up. MythTV has no knowledge
of these external needs and so I decided to build that logic into the
two scripts (See attached)

'SetWakeupTime' (Sorry, I have a penchant for capitalization) will
maintain a list of wake up times for the system and will set the system
wake up time to the earliest one in the list. Any script that wants to
scheduled wake up time will call this script as root (sudo) to add that
time to the list.

When any cronjob in running it will 'touch' /tmp/StopSleep to prevent
the system from going to sleep and remove the file in the end to
initiate sleep.

'CheckState' will check for this file, or see if there are any users
logged in or if there is a wake up due in the next 15 minutes. If so it
will return non-zero to prevent MythTV from putting the system to sleep.
Otherwise it will return zero, letting MythTV know that the system can
go into hibernation.

One thing I need to do is to move the list to a persistent location
(instead of /tmp) so that the list can be maintained after restart/shutdown.

Comments or suggestions are welcome.

Thanks
-------------- next part --------------
#!/bin/bash
#
# Script to check 
#   1) if a file (/tmp/StopSleep) exists or 
#   2) if someone is logged in
#   3) if the next wakeup time  is soon  (<15 minutes)
#
# and return 1 - Otherwise return zero
#
#set -xv
#
#   if /tmp/StopSleep exists, don't go to sleep
#
if [ -f /tmp/StopSleep ]; then
   exit 1
fi
#
#  If someone is logged in, don't go to sleep
#
if ! who -q | grep "users=0"; then
   exit 1
fi
#
# Check if WDTV is up - Does not work 
#
#WDTV=$(arp |grep -i 00:90:a9:c6:40:34)
#if [ -z "$WDTV" ]; then
#   exit 1
#fi
#
# Check to see if the next wake up time is soon (<15 mins)
# then don't allow the system to go to sleep
#
CURTIME=`date +%s`
rm -rf /tmp/WakeupTimes.tmp
sort -n -u /tmp/WakeupTimes >> /tmp/WakeupTimes.tmp
while read NEWTIME; do
  echo $NEWTIME
  if [ $NEWTIME -gt $CURTIME ]; then
     DIFF=`expr ${NEWTIME} - ${CURTIME}`
     if [ $DIFF -lt 900 ]; then
        exit 1
     fi
     break
  fi
done < /tmp/WakeupTimes.tmp
#
# You can go to sleep
#
exit 0
-------------- next part --------------
#!/bin/sh
#
# Script to set wake up time to $1 (given in epoch time)
#
#  Date         Version   Description
# Oct 10,2011   1.0       Intial Version
# Mar 17,2014   1.1       updated to keep a list of wakeup times and 
#                         use the earliest to wakeup
#
#set -xv
#
# Add current to the list of wakeup times
#
TIME=$1
touch /tmp/WakeupTimes
echo $TIME >> /tmp/WakeupTimes
rm -rf /tmp/WakeupTimes.tmp
sort -n -u -r /tmp/WakeupTimes >> /tmp/WakeupTimes.tmp
CURTIME=`date +%s`
rm -rf  /tmp/WakeupTimes
touch /tmp/WakeupTimes
while read NEWTIME; do
  echo $NEWTIME
  if [ $NEWTIME -gt $CURTIME ]; then
     echo $NEWTIME >> /tmp/WakeupTimes
     TIME=$NEWTIME
  fi
done < /tmp/WakeupTimes.tmp
rm -rf /tmp/WakeupTimes.tmp
#
# If your machine is set to local time you will need the following adjustment
# Because of windows we need to switch from UTC to local time in h/w clock
# Note DIFF is not constant because of daylight savings time change
#
# Otherwise set EPOCH=$TIME
#
TODAY=`date +%F`
DIFF=`expr \`date --date "$TODAY 07:00:00" +%s \` - \`date --date "$TODAY 07:00:00" +%s -u\``
EPOCH=`expr ${TIME} - $DIFF`
#
# Set Wake up time - Make the log file readable by non-root
#
WAKETIME=`date --date "@${TIME}" `
CURTIME=`date `
touch /tmp/SetWakeupTime.log
chmod 644 /tmp/SetWakeupTime.log
echo Current Time = $CURTIME - Wakeuptime = $WAKETIME - Input Time = $1>> /tmp/SetWakeupTime.log
echo 0 > /sys/class/rtc/rtc0/wakealarm
echo $EPOCH > /sys/class/rtc/rtc0/wakealarm 


More information about the mdlug mailing list