#!/usr/bin/perl ######################################################### # Ryan Pisani $dailywake && $recday == $day && $hour < $dailywake ) { #Set rechour to dailywake time $rechour=$dailywake; } #If the recday is less than today, it's in the next month #if it's greater than today, it's still in this month #either way, if either of these conditions exist. we'll se #today for dailywake if ( ($recday < $day || $recday > $day) && $dailywake ne "" && $hour < $dailywake ) { $recday=$day; } #Go ahead and adjust for wakeup boot time now if ( $recmin < 06 ) { $recmin='52'; $rechour=$rechour - 1; } else { $recmin=$recmin - 10; } #Check to see what time the recording is scheduled if ( "$recday" eq "$day" ) { my $hourdiff=$rechour - $hour; #If more than 1 hour away we check the shutdown if ( "$hourdiff" > "1" || "$hourdiff" < "-1" ) { #print "More than 1 hour\n"; $checkshutdown="1"; } } else { #We'll check shutdown since it's tomorrow $checkshutdown="1"; } #Now we see what things are set to. If conditions exist we'll check dailywake if ( $dailywake ne "" && $rechour > $dailywake && ($recday < $day || $recday > $day) ) { $waketimeset="0"; } else { #Set a flag to tell us if we need to set dailywake or not $waketimeset="1"; } #We'll exit the loop on the first match last; } } #print "$waketimeset\n"; #If for some reason there is no recordings in the forseeable future I'll go ahead and catch #here and set the wakeup time for dailywake if enabled. if ( $waketimeset ne "1" && $dailywake ne "" ) { #If our current hour is greater than the dailywake #We need to adjust the day to wake up if ( $hour >= $dailywake ) { #We'll add to epoch time to increase date and set acpitime my $epoch=`date +%s`; my $tomepoch=$epoch + 86400; my $tomdate=`date -d '1970-01-01 $tomepoch sec GMT' +%Y-%m-%d\\ %T | awk {'print \$1'}`; chomp(($month,$recday)=(split /-/, $tomdate)[1,2]); #print "recorday=$recday\n"; } else { $recday=$day; } #We need to check to see if the dailywake is in the past #if so, we should increase the day by 1 so it wakes tomorrow $rechour="$dailywake"; $recmin="00"; #We'll turn checkshutdown on too $checkshutdown="1"; } #Now we set ACPI time regardless if shutdown is merited my $acpitime="$year-$month-$recday $rechour:$recmin:00"; #print "here: $acpitime checkshutdown: $checkshutdown\n"; open(ALARM,">/proc/acpi/alarm"); print ALARM "$acpitime"; close ALARM; #Now let's see if we should even check for shutdown at this time if ( $checkshutdown ne "" ) { checkshutdown(); } sub checkshutdown { #Check to see machine uptime. Maybe it was started and we just haven't #started the frontend yet. chomp(my $uptime=`cat /proc/uptime | awk -F"." '{print \$1}'`); #Now let's check shutdown status and see if it's ok to turn the machine off my $error=system("mythshutdown --check"); #check to see what mythshutdown says is the stats if ( $error eq 0 && $uptime > 900 && ($hour <= $dailywake || $hour >= $dailydown)) { #Now check to see if the frontend or backend is stillrunning my $frontactive=`lsof /dev/dsp | grep mythfront`; my $frontcheck=`ps -ef | grep -w mythfrontend | grep -v grep`; my $filecheck=`lsof $recordpath/* | grep -v lock`; #Now we'll check for mplayer & xine my $xinecheck=`ps -ef | grep -w xine | grep -v grep`; my $mplayer=`ps -ef | grep -w mplayer | grep -v grep`; #print "frontcheck:$frontcheck\nfilecheck:$filecheck\nxinecheck:$xinecheck\nmplayer:$mplayer\n"; #So on a couple of conditions we drop out of shutdown. #If the frontend is on and running, or if lsof reports a file in use in the video dir if ( ($frontcheck ne "" && $frontactive ne "") || $filecheck ne "" || $xinecheck ne "" || $mplayer ne "" ) { #bomb out #print "exiting\n"; exit; } else { #if we've already tested for shutdown and we land here again #we may as well let it shutdown now. if ( $checkitagain eq '1' ) { #Lets log the acpi time now that we'll shutdown open( LOG,">>/var/log/acpiwakeup.log"); print LOG "Current Time: $nowtime Wakeup Time: $acpitime\n"; close LOG; #Sync the board clock to the system clock for good sleeping system("hwclock --systohc"); #All clear if we get here, so let's shut the host down system("shutdown -h now"); } else { #Set the flag if it's the first time we've come through $checkitagain='1'; #print "$acpitime\n"; sleep 180; checkshutdown(); } } } }