[mythtv-users] Auto Shutdown and Startup

Jan Dölle myth at jan-doelle.de
Sat Oct 2 04:26:58 EDT 2004


Hi Dan,

This may be a good point to start, but is not finish.

The script assumes that you using nvram + grub for wakeup schedule. Also sudo for root 
only commands must setup

Use the --setwakeup, --shutdown and --check in your myth-setup.
If you autostart your mythfrondend on systenstart, you can check with --startup, if the system 
was startup scheduled or manually. (Work not in any case, some work to do).

--lock and --unlock is usefull to prevent mythbackend from shutting down.

Sorry for that trashy answer, but I am in hurry ;-)

Jan


---- grub
default  saved

...
title  VDR 
root  (hd0,1)
kernel  /boot/vmlinuz-2.4.26-1-k7-vdr root=/dev/hda2 ro hdc=ide-scsi noapic apic=off video=vc:0-0 
initrd  /boot/initrd.img-2.4.26-1-k7-vdr
savedefault
boot

title  PowerOff
#savedefault  0 
savefefault --default=0
halt
...

----- mythshudown ---
#!/bin/bash
# shutdown and wakeup script for mythbackend
# by Jan Doelle (tuxnewbie) rev 0.4 28.07.2004

NAME="mythshutdown"
USER="mythtv"

#set this option if your bios not support date
WAKEUP_DAILY="07:00"

CONFIG_FILE=/etc/mythshutdown.conf

MYTH_LOG_FOLDER=/var/log/mythtv
MYTH_TMP_FOLDER=/var/mythtv

MYTH_LOG_FILE=$MYTH_LOG_FOLDER/mythshutdown.log

NVRAMCMD=/usr/sbin/nvram-wakeup
NVRAMCFG=/etc/nvram-wakeup.a7n8x-x                  # avoid using default nvram-wakeup.conf delieved by nvram-wakeup package

MYTH_SCHEDULE_TIME_FILE=$MYTH_TMP_FOLDER/myth-schedule-time    # contains time when mythbackend wants start up to record
MYTH_SCHEDULE_VRWU_FILE=$MYTH_TMP_FOLDER/myth-schedule-vrwu    # contains time when system will wakeup next
MYTH_SCHEDULE_FLAG_FILE=$MYTH_TMP_FOLDER/myth-schedule-flag    # flag file will create be
MYTH_SHUTDOWN_LOCK_FILE=$MYTH_TMP_FOLDER/myth-shutdown-lock    # if exists mythshudown --check will return 1 reset idle count

#prepare tmp folder
mkdir -p $MYTH_TMP_FOLDER
chown -R $USER $MYTH_TMP_FOLDER

#
# log function
#

log () {
    stamp=$(date +"[%m-%d-%Y %H:%M:%S]")
    echo "$stamp $1" >> $MYTH_LOG_FILE
    
    #debug
    echo "$stamp $1"
}

#
# Overwrite Default Settings
#
if [ -e $CONFIG_FILE ]; then
 . $CONFIG_FILE
fi

#
# check nvram-wakeup exists
#

# test -x $NVRAMCMD || exit 1

EXITSTATUS=1

case "$1" in
   
   --lock)
      log "lock shutdown"
           
      if [ ! -f $MYTH_SHUTDOWN_LOCK_FILE ]; then
         cat /dev/null > $MYTH_SHUTDOWN_LOCK_FILE
         chmod a+rw-x $MYTH_SHUTDOWN_LOCK_FILE
         chown $USER $MYTH_SHUTDOWN_LOCK_FILE
         chgrp $USER $MYTH_SHUTDOWN_LOCK_FILE
      else
         log "allready locked"
      fi
      EXITSTATUS=0
   ;;
   --unlock)
      log "unlock shutdown"
     
      if [ -f $MYTH_SHUTDOWN_LOCK_FILE ]; then
         rm $MYTH_SHUTDOWN_LOCK_FILE
         if [ $? != 0 ]; then
            log "unable to unlock shutdown"
         fi
      else
         log "wasn't locked"
      fi
      EXITSTATUS=0
   ;;
   --check)
      log "check shutdown"
      
      # mythbackend wants 0=ok shutdown, 1=reset idlecount, 2=wait for frontend
      
      if [ -f $MYTH_SHUTDOWN_LOCK_FILE ]; then
         # echo "Locked"
         log "shutdown is locked"
         EXITSTATUS=1
      else
         # echo "Unlocked"
         log "free to shutdown" 
         EXITSTATUS=0
      fi
   ;;
   --setwakeup)
      log "set wakeup time. given is $2"
      
      WAKEUPTIME=$(date --date="$2" +"%R %F")
      log "wakeup time is: $WAKEUPTIME"
      
      if [ $? = 0 ]; then
         echo "$WAKEUPTIME" > $MYTH_SCHEDULE_TIME_FILE
         if [ $? = 0 ]; then
            chmod a+rw-x $MYTH_SCHEDULE_TIME_FILE
            chown $USER $MYTH_SCHEDULE_TIME_FILE
            chgrp $USER $MYTH_SCHEDULE_TIME_FILE
         else
            log "writing $MYTH_SCHEDULE_TIME_FILE failed"
         fi
         
         echo "$WAKEUPTIME" > $MYTH_SCHEDULE_FLAG_FILE
         if [ $? = 0 ]; then
            chmod a+rw-x $MYTH_SCHEDULE_FLAG_FILE
            chown $USER $MYTH_SCHEDULE_FLAG_FILE
            chgrp $USER $MYTH_SCHEDULE_FLAG_FILE
         else
            log "writing $MYTH_SCHEDULE_FLAG_FILE failed"
         fi
         EXITSTATUS=0
      else
         # error in format
         log "--setwakeup: invalid date format ($2) must be hh:mm yyyy-MM-dd"
         EXITSTATUS=1
      fi
      # echo "WAKEUPTIME: $WAKEUPTIME $?"
   ;;
   --shutdown)
      log "shutdown"
      # now the horror starts
      # check 
      #NVWAKEUPTIME=0
      
      # current time
      TSNOW=$(date +%s)
      
      # daily wakeup time
      TSDAY=$(date -d "$WAKEUP_DAILY" +%s)
      if [ "$TSDAY" -gt "$TSNOW" ]; then
         log "daily wakeup today at $WAKEUP_DAILY"
      else
         # next daily is tommorrow
         log "daily wakeup tomorrow at $WAKEUP_DAILY"
         TSDAY=$(( $TSDAY + (24*60*60) ))
      fi
      
      if [ "$TSDAY" -lt $(($TSNOW + 15*60)) ]; then
      # forward daily 15 minutes (avoid to short time for wakeup shedule)
  log "daily wakeup delayed about 15 minutes"
  TSDAY=$(($TSDAY + 15*60))
      fi     
      # check schedule
      if [ -f $MYTH_SCHEDULE_FLAG_FILE ]; then
         
         # a wakeup was scheduled
         if [ -f $MYTH_SCHEDULE_TIME_FILE ]; then
         
            WAKEUPTIME=$(cat $MYTH_SCHEDULE_TIME_FILE) # | awk '{print $1}') 
            log "wakeup scheduled at: $WAKEUPTIME"
            
            TSWKU=$(date -d "$WAKEUPTIME" +%s)
            TSDELTA=$(($TSWKU-$TSNOW))
            
            if [ "$TSDELTA" -lt "0" ] ; then
               log "schedules in pass. schedule deleted"
               rm $MYTH_SCHEDULE_TIME_FILE
        log "wakeup at daily wakeup time $WAKEUP_DAILY ($TSDAY)"
        NVWAKEUPTIME=$TSDAY
            elif [ "$TSWKU" -lt "$TSDAY" ]; then
               log "wakeup at scheduled time $WAKEUPTIME ($TSWKU)"
               NVWAKEUPTIME=$TSWKU
            elif [ -n "$TSDAY" ]; then
               log "wakeup at daily wakeup time $WAKEUP_DAILY ($TSDAY)"
               NVWAKEUPTIME=$TSDAY
            else 
               log "not daily walkuptime"
               # panic
            fi
         fi
         echo "$NVWAKEUPTIME" > $MYTH_SCHEDULE_VRWU_FILE
         if [ $? = 0 ]; then
            chmod a+rw-x $MYTH_SCHEDULE_VRWU_FILE
            chown $USER $MYTH_SCHEDULE_VRWU_FILE
            chgrp $USER $MYTH_SCHEDULE_VRWU_FILE
         else
            log "writing $MYTH_SCHEDULE_VRWU_FILE failed"
         fi

         # delete flag file
         rm $MYTH_SCHEDULE_FLAG_FILE
      else
         # no wakeup was set
         log "no wakeup time set"
         if [ -n "$TSDAY" ]; then
            log "wakeup at daily wakeup time"
            NVWAKEUPTIME=$TSDAY
         else
            NVWAKEUPTIME=0
         fi
      fi
      
      # stop here to debug
      # exit 0
      
      SHUTDOWNMODE=2
      
      if [ -n $NVWAKEUPTIME ]; then
         log "$NVRAMCMD --configfile "$NVRAMCFG" --syslog --settime $NVWAKEUPTIME"
         sudo $NVRAMCMD --configfile "$NVRAMCFG" --syslog --settime "$NVWAKEUPTIME"
         SHUTDOWNMODE=$?
         log "$NVRAMCMD exited with code $SHUTDOWNMODE"
         log "Currently we don't trust exit code 0, forcing to 1..."
         SHUTDOWNMODE=1
      fi
  
      case "$SHUTDOWNMODE" in
         0)
            log "everthing looks fine, shutting down ..."
            sudo shutdown -h now
            EXITSTATUS=0
         ;;
         1)
            log "everthing looks fine, but reboot is needed"
            log "sending command to grub ..."
            echo "savedefault --default=1 --once quit" | sudo grub
     log ".."
     log "."
            log "shutting down ..."
            sudo shutdown -r now
            EXITSTATUS=0
         ;;
         *)
            log "panic. invalid shutdown mode, do nothing"
            EXITSTATUS=1
         ;;
      esac
   ;;
   --startup)
      # check startup at scheduled time 0 yes 1 no (manualy)   
      if [ -f $MYTH_SCHEDULE_VRWU_FILE ]; then
         TS_WAKEUP=$(cat $MYTH_SCHEDULE_VRWU_FILE) # | awk '{print $1}') 
         TS_NOW=$(date +%s)
         TS_DELTA=$(($TS_WAKEUP - $TS_NOW))
         if [ $TS_DELTA -gt $((15*60)) ]; then
            log "startup manualy"
            EXITSTATUS=1
         else
            log "startup automaticly"
            EXITSTATUS=0
         fi
      else
         log "startup manualy"
         EXITSTATUS=1
      fi
   
   ;;
   *)
   #usage
      echo "Usage of $NAME"
      echo "--setwakeup time (time=hh:mm yyyy-MM-dd sets the wakeup time. don't write it into nvram)"
      echo "--shutdown       (shutdown set nvram-wakeup and shutdown)" 
      echo "--startup        (check startup. check will return 0 if automatic else 1 for manualy)"
      echo "--check          (check shutdown possible returns 0=ok and 1=reset idle check)"
      echo "--lock           (disable shutdown. check will return 1)"
      echo "--unlock         (enable shutdown. check will return 0)"
    ;;
esac

if  [ "1" -eq "0" ]; then
   echo "Diagnostics"
   echo "$MYTH_TMP_FOLDER"
   ls -l $MYTH_TMP_FOLDER
   echo "$MYTH_LOG_FOLDER"
   ls -l $MYTH_LOG_FOLDER
fi
exit $EXITSTATUS


Am Samstag, 2. Oktober 2004 07:03 schrieb DCL:
> Mythtv will only auto shutdown if the frontend appliction is running.  
> How can mythtv be set to shutdown after a scheduled recording when it is 
> set to autostart if the front end is also set to autostart when the 
> application is started by the bios clock?
> 
> I have tried a script the should be executed immediately after the 
> backend starts that checks the $status value, if "auto" it does nothing, 
> if "user" it should start mythfrontend. 
> 
> This does not work.  Any ideas?
> 
> Thanks Dan
> 
> 
> _______________________________________________
> mythtv-users mailing list
> mythtv-users at mythtv.org
> http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-users
> 


More information about the mythtv-users mailing list