#! /bin/sh # # Written by Craig Huff # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # ## Return Value Meaning ## 0 Ok to shut down ## 1 Something going on -- do not shut down # ## Define a constant for the maximum amount of time difference, in seconds, ## that we want to allow between the current time and the time that zap2it ## suggested for the next database update. (5 minutes x 60 seconds) margin=300 # Does mythshutdown say it is ok to shutdown? /usr/bin/mythshutdown --check result=$? # returns 0 if mythshutdown has no reason to stay up if [ $result -ne 0 ]; then # if mythshutdown says to stay up, exit $result # we don't need to do any more checking. fi # Check for mythburn running and capture result of check ps -ef|grep -v grep|grep mythburn >& /dev/null # grep returns 0 if it found any matches if [ $? -eq 0 ]; then # if grep found any matches, stay up. exit 1 # we don't need to do any more checking. fi # Check if mythfilldatabase is supposed to run within ${margin} seconds from now ## First, get the time zap2it suggested as the next update time from the log nexttime=`cat /var/log/mythtv/mythfilldatabase.log.1 /var/log/mythtv/mythfilldatabase.log | grep NextSuggestedTime | tail -1 | awk '{print $NF}' | sed -e 's/T/ /'` ## Convert it into the number of seconds since the "epoch" epochtime=`date -u -d "${nexttime}" +%s` ## Next, get the current time in seconds since the "epoch" thistime=`date +%s` ## If the time has not already passed AND we are close enough (<= ${margin}) then stay up if [ ${epochtime} -gt ${thistime} ]; then ## Subtract the larger time from the smaller to get a positive difference difftime=`expr ${epochtime} - ${thistime}` if [ ${difftime} -le ${margin} ]; then exit 1 fi fi # # Check for mythfilldatabase running and capture result of check pgrep mythfilldatabase >& /dev/null # pgrep returns 0 if it found any jobs that match supplied criteria if [ $? -eq 0 ]; then # if pgrep found any jobs, stay up. exit 1 # we don't need to do any more checking. fi # Now, if there are any pseudo-terminals in use then do not shutdown if [ `ps -ef | egrep "pts/[0-9]" | egrep -v egrep | wc -l` -ne 0 ]; then exit 1 # Stay up. else exit 0 # Shut down. fi