Advertisement
Protricity

sleeptest.sh

Dec 28th, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.37 KB | None | 0 0
  1. #!/bin/sh
  2. # Suspends and Resumes XBMC based on Monitor status
  3. # By Ari Asulin ([email protected])
  4. # */15 * * * * /etc/init.d/sleeptest.sh
  5.  
  6.  
  7. if [ $(pidof -x sleeptest.sh| wc -w) -gt 2 ]; then
  8.     echo "Script currently running. Exiting..."
  9.     exit
  10. fi
  11.  
  12. cmdMonitor="/opt/vc/bin/tvservice -s"   # command to check monitor status
  13. cmdWake="/sbin/initctl start xbmc"      # command to resume xbmc
  14. cmdSleep="/sbin/initctl stop xbmc"      # command to suspend xbmc
  15.  
  16. evtSleep="HPD low"                      # change this based on the signals your monitor sends
  17. evtWake="HPD high"                      # when turned on and off
  18.  
  19. sleepDelay=30                           # Waiting period after monitor off. Must be < 60
  20. checkDelay=5                            # 5 Seconds = average of 2.5 second response
  21.  
  22. echo "Starting XBMC HDMI Monitor..."
  23. echo "  If this hangs with no output, most likely tvservice is broken and you'll need to restart it"
  24.  
  25. while true; do # Forever loop
  26.  
  27.         # Get XBMC PID if its running
  28.         PID=`ps -A | grep xbmc.bin | awk '{print $1}'`
  29.  
  30.         # Check for Monitor Off Signal
  31.         if $cmdMonitor | grep "$evtSleep" > /dev/null; then
  32.  
  33.                 # Monitor is Off, check to see if XBMC is running
  34.                 echo "HDMI Monitor is Off"
  35.                 if [ $PID ]; then
  36.  
  37.                         # XBMC is running with monitor off, so wait a bit
  38.                         echo "XBMC is running [$PID]. Waiting $sleepDelay Seconds..."
  39.                         sleep $sleepDelay
  40.  
  41.                         # Check to see if the monitor is still off
  42.                         if $cmdMonitor | grep "$evtSleep" > /dev/null; then
  43.  
  44.                                 # The monitor didn't come back on, so suspend
  45.                                 echo "HDMI Monitor is still off. Suspending XBMC..."
  46.                                 $cmdSleep
  47.                         else
  48.  
  49.                                 # Looks like the monitor came back on while we were waiting, so do nothing.
  50.                                 echo "HDMI Monitor came back on during waiting period. Doing nothing..."
  51.                         fi
  52.                 else
  53.  
  54.                         # XBMC is not running and the monitor is off, so do nothing.
  55.                         echo "XBMC is not running"
  56.                 fi
  57.  
  58.         # Check for Monitor On Signal
  59.         elif  $cmdMonitor | grep "$evtWake" > /dev/null; then
  60.  
  61.                 # Monitor is On, check to see if XBMC is running
  62.                 echo "HDMI Monitor is On"
  63.                 if [ $PID ]; then
  64.  
  65.                         # XBMC is running, so we can exit
  66.                         echo "XBMC is running [$PID]. Exiting..."
  67.                         sleep 60 # We're done, so sleep for a while
  68.  
  69.                 else
  70.  
  71.                         # XBMC is not running, but the monitor is on, so resume xbmc
  72.                         echo "XBMC is not running. Resuming..."
  73.                         $cmdWake
  74.                         sleep 60 # We're done, so sleep for a while
  75.                 fi
  76.         else
  77.  
  78.                 # If you get this status, run tvservice -s with the monitor on and off
  79.                 # to figure out what strings to look for
  80.                 echo "HDMI Monitor Status Unknown"
  81.         fi
  82.  
  83.         # If nothing else is going on, sleep
  84.         echo "Sleeping $checkDelay Seconds..."
  85.         sleep $checkDelay
  86. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement