Difference between revisions of "Sleep(Suspend) When Idle"

From MythTV Official Wiki
Jump to: navigation, search
(Suspend your machine when idle.)
Line 46: Line 46:
[[Category:Power Management]]
[[Category:Power Management]]
[[Category:Python Scripts]]

Revision as of 06:24, 14 September 2012

The script below can be used to put a frontend to sleep whenever xscreensaver turns the display 'Off' via DPMS. It was written with Mythbuntu 12.04/XFCE in mind, however it could probably be adapted for others distros.

Every 30 seconds it checks if mythfrontend is running and no additional users are logged in (via SSH), if so, it will check the DPMS status via xset, and if it is 'Off' it will suspend the machine.

Make the script executable by your frontend user account, and configure it to run in the session start.

NOTE: DPMS has 4 states; on, sleep, suspend, and off. The script will only trigger on the 'Off' state.

Script.png autosuspend.py
#!/usr/bin/env python
from subprocess import call, check_output
import time

# Mythtv frontend process name
fecommand = "mythfrontend.real"

# Command to use to suspend the computer
sleepcmd= ('dbus-send --system --print-reply '
           '--dest="org.freedesktop.UPower" '
           '/org/freedesktop/UPower '

# Start infinate loop
while True :

  # Wait 30 seconds before performing checks
  time.sleep (30)

  # Determine if the Frontend is stopped
  festopped = call("pidof " + fecommand + " >nul", shell=True)

  # Get the number of logged in users
  numusers = check_output("who -q | grep users | awk -F'=' '{print $2}'", shell=True)

  # If the number of logged in users <2 and the front end is not stopped
  if int(numusers) < 2 and not festopped:

    # Get the monitor DPMS status
    status = check_output('xset q |grep "Monitor is "', shell=True)

    # If the monitor DMPS status is 'Off'
    if 'Off' in status:

      # Run the sleep command
      call(sleepcmd, shell=True)