#! /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. # logfile=/var/log/Shutdown.log now=`date` me=`basename $0 | sed -e 's/^\.//'` echo >> $logfile echo >> $logfile echo >> $logfile # The next few lines are useful for debugging until you have the scripts #working, in case I forgot to mention something ;-). # echo "$me requested $now" >> $logfile echo "$me running as:" >> $logfile ps -ef | grep $me >> $logfile echo \"whoami\" reports: `/usr/bin/whoami` >> $logfile ps -ef | grep $$ >> $logfile # Now log pre-shutdown memory statistics in case there's a problem with #hibernation working echo >> $logfile vmstat -S K >> $logfile # Turn off mythwelcome here since I had trouble with hibernation when it #was running. # There's probably a way to do this within the framework of Suspend2, #but, if it ain't broke, don't... # echo >> $logfile pkill -x mythwelcome # Capture the result so we know on reboot if it was running (we succeeded #in killing it) or not (didn't succeed). result=$? echo $result > /tmp/mythwelcome_killed # And for debugging purposes, log the same information with an explanation. echo mythwelcome pkill result saved = $result >> $logfile echo 'If result is 0, it means mythwelcome kill succeeded.' >> $logfile # Now do the same thing for irexec, for the same reasons. pkill -x irexec result=$? echo $result > /tmp/irexec_killed echo irexec pkill result saved = $result >> $logfile echo 'If result is 0, it means irexec kill succeeded.' >> $logfile # And again for mythbackend. pkill -x mythbackend result=$? echo $result > /tmp/mythbackend_killed echo mythbackend pkill result saved = $result >> $logfile echo 'If result is 0, it means mythbackend kill succeeded.' >> $logfile # Did the memory statistics change? Log them in case it matters. echo >> $logfile vmstat -S K >> $logfile # Get a backup of the mythtv mysql database before every shut down and #use logrotate to keep a few copies around. Might prove useful someday ;-) /usr/bin/mysqldump -u mythtv -pmythtv mythconverg -c > /var/log/mysql/mythtv_backup.sql # At last, we're ready to let the hibernate command do it's thing! echo >> $logfile /usr/sbin/hibernate -v3 >> $logfile # When we reach this point in the script, we've hibernated and restarted again. # The Suspend2 code has restored from disk (or ram) and picked up where it left #off in this script. result=$? # returns 2 if hibernate was aborted due to errors in script echo hibernate result returned = $result >> $logfile # In the reverse order of the sequence we killed them (if they were running) #restart mythbackend, irexec, and mythwelcome. # # Was mythbackend running? read killed_mythbackend < /tmp/mythbackend_killed \rm /tmp/mythbackend_killed echo mythbackend pkill result recovered = $killed_mythbackend >> $logfile # Was irexec running? read killed_irexec < /tmp/irexec_killed \rm /tmp/irexec_killed echo irexec pkill result recovered = $killed_irexec >> $logfile # Was mythwelcome running? read killed_mythwelcome < /tmp/mythwelcome_killed \rm /tmp/mythwelcome_killed echo mythwelcome pkill result recovered = $killed_mythwelcome >> $logfile # If mythbackend was running, restart it if [ 0 -eq $killed_mythbackend ]; then echo starting mythbackend now >> $logfile service mythbackend start >> $logfile fi # If irexec was running, restart it if [ 0 -eq $killed_irexec ]; then echo starting irexec now >> $logfile su -c "irexec > /dev/null 2>&1 &" - mythtv fi # If mythwelcome was running, restart it if [ 0 -eq $killed_mythwelcome ]; then #but give everything else a chance to get on both feet sleep 2 echo starting mythwelcome now >> $logfile #and don't forget that we want mythwelcome running as user mythtv, not root! #Because of that, we'll need to tell it where to display, too. su -c "export DISPLAY=:0.0;mythwelcome > /dev/null 2>&1 &" - mythtv fi #Give anacron a chance to run periodic jobs each time the suspended system restarts #(useful for cron-type jobs that would get missed while the system is hibernating, #such as mythfilldatabase, perhaps?) Note that while this is autostarted in the #/etc/init/rc?.d structure, this doesn't happen when waking up from hibernation -- #we're restoring to a running state, not "rebooting", remember? service anacron start >> $logfile #Finally, ensure that mythfilldatabase gets run at the appointed time /usr/local/bin/fillmythdatabase >> $logfile 2>&1 &